var textSize = 10.0;
// or
double textSize = 10.0;
Run Code Online (Sandbox Code Playgroud)
入Text颤振小部件
child: const Text('Calculate Client Fees',
style: TextStyle(fontSize: textSize),)
Run Code Online (Sandbox Code Playgroud)
在这里给错误
无效的常数值
我们必须强制const使用价值吗?为什么我们不能使用var或double?
Muh*_*qib 47
如果不使用固定值,请勿使用const关键字。例如,在 的情况下Text,如果它的字符串是常量,Text("something here")那么我们应该使用 the const,但是如果 的字符串Text是动态的,那么不要在Textwidget 之前使用 const。
所有小部件的情况都是如此const Text("something")。Text(anyVariabale)
Del*_*des 12
在 dart 中,当您在 const 构造函数中传递某些内容作为参数时,编译器会确保设置为默认值的值在代码执行期间不会更改。
因此,出现无效常量值警告。
要解决此问题,您应该从文本前面删除 const 关键字。
cre*_*not 10
您正在将Text小部件声明为const,这也要求其所有子代const也是如此。如果要解决此问题,const Text在这种情况下,由于要传递非常量变量,因此不应使用小部件。
原因是Flutter使用const关键字作为小部件的标识符,因为它永远不会在编译时进行评估,因为它只会在编译时被评估一次。因此,它的每个部分也必须是恒定的。
double textSize = 10.04;
// ...
child: Text('Calculate Client Fees', style: TextStyle(fontSize: textSize))
Run Code Online (Sandbox Code Playgroud)
在本文中阅读有关它的更多信息。
小智 8
child: const Text('Calculate Client Fees',
style: TextStyle(fontSize: textSize),)
Run Code Online (Sandbox Code Playgroud)
应该:
child: Text('Calculate Client Fees',
style: TextStyle(fontSize: textSize),)
Run Code Online (Sandbox Code Playgroud)
正如@creativecreatorormaybenot所说,您正在使用const Text()这就是为什么您必须在const那里拥有价值。您可以使用
const double textSize = 10.0;
Run Code Online (Sandbox Code Playgroud)
或者
const textSize = 10.0;
Run Code Online (Sandbox Code Playgroud)
就像这个案例一样。
Padding(
padding: const EdgeInsets.all(value), // this value has to be a `const` because our padding: is marked const
child: Text("HI there"),
);
Padding(
padding: EdgeInsets.all(10), // any double value
child: Text("HI there"),
);
Run Code Online (Sandbox Code Playgroud)
您应该删除关键字const
当它被使用时,它会将小部件放入缓存中。
而且你不能等待一个值进入你的小部件并再次将小部件作为常量。当你想这样做时,你不应该让你的小部件保持不变。
所以这样做:
double textSize = 10.0;
child: Text('Calculate Client Fees', style: TextStyle(fontSize: textSize),)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1580 次 |
| 最近记录: |