在创建小部件树时,在静态小部件之前插入 const
会提高性能吗?
即
child: const Text('This is some text');
对
child: Text('This is some text');
我知道,在 Dart 2 中,const
是可选的,并且会在某些地方自动插入。这是其中一种情况吗?如果不是,使用 const
会减少内存使用/提高性能吗?
感谢您的回答!
最佳答案
这是一个小的性能改进,但它可以添加到较大的应用程序或经常重建 View 的应用程序中,例如由于动画。
const
减少了垃圾收集器所需的工作。
您可以在 analysis_options.yaml
中启用一些 linter 规则,告诉您何时应该添加 const
,因为它不是推断出来的,但可能像
或者当你使用 const
时会提醒你,但无论如何都是推断出来的
另见 https://www.dartlang.org/guides/language/analysis-options
https://stackoverflow.com/questions/53492705/