flutter开发中Use ‘const’ with the constructor to improve performance. Try adding the ‘const’ keyword to the constructor invocation.报错如何解决-优雅草卓伊凡
问题背景
flutter开发中Use ‘const’ with the constructor to improve performance. Try adding the ‘const’ keyword to the constructor invocation.
一个关于使用const关键字的警告。这个警告表示你可以在一些地方使用const关键字来提高性能。在Flutter中,如果一个小部件的所有参数都是常量,那么将其声明为const可以让Flutter更高效地重用它。
解决方案
可以在以下地方添加const关键字来解决这个警告:
诸如:
DropdownButtonFormField 的 items 参数
TextField 构造函数
InputDecoration 构造函数
Text 构造函数
等等
做个例子:
Text( "Please register your account", style: TextStyle( color: Colors.white, fontSize: 32, fontFamily: "PingFang SC", fontWeight: FontWeight.w800, ),
这样的代码 flutter就会提示警告
const Text( "Please register your account", style: TextStyle( color: Colors.white, fontSize: 32, fontFamily: "PingFang SC", fontWeight: FontWeight.w800, ),
如果是这样,这个问题就能解决-ok 完美