dart 合并文本和一个双在 Flutter

vc6uscn9  于 9个月前  发布在  Flutter
关注(0)|答案(2)|浏览(114)

我需要在文本小部件中显示文本和双精度,但我得到一个错误。这应该是一个简单的事情。
代码如下:

double commission = 0.0;

const Text('Commission: ${commission}'),

字符串
这里是错误

A value of type 'Null' can't be assigned to a parameter of type 'String' in a const constructor.
Try using a subtype, or removing the keyword 'const'.dartconst_constructor_param_type_mismatch
Unnecessary braces in a string interpolation.
Try removing the braces.dartunnecessary_brace_in_string_interps
Invalid constant value.dart(invalid_constant)


我尝试什么都不起作用。我初始化了变量,但它仍然不起作用。
这应该很容易,但我如何修复它?谢谢

h43kikqp

h43kikqp1#

删除const,因为您正在尝试在小部件中嵌入动态值,以便其内容可以在无法使用const的地方动态更改。

vfh0ocws

vfh0ocws2#

无法添加具有动态值的“const”。

class MyHomePage extends StatelessWidget {
  final String title;
  const MyHomePage({super.key, required this.title});

  @override
  Widget build(BuildContext context) {
    double commission = 0.0;
    return Scaffold(
      body: Center(
        child: Text('Commission : ${commission}'),
      ),
      appBar: AppBar(
        // The title text which will be shown on the action bar
        title: Text(title),
      ),
    );
  }
}

字符串

相关问题