创建widget树时,会插入const
在静态小部件提高性能之前?
ie
child: const Text('This is some text');
vs
child: Text('This is some text');
我知道,使用 Dart 2,const
是可选的,将在某些地方自动插入。这是其中一种情况吗?如果不是,将使用const
减少内存使用/提高性能?
这是一个很小的性能改进,但它可以在较大的应用程序或经常因动画而重建视图的应用程序中累积起来。
const
减少垃圾收集器所需的工作。
您可以启用一些 linter 规则analysis_options.yaml
告诉你什么时候应该添加const
因为它不是推断出来的,但有可能像
- http://dart-lang.github.io/linter/lints/prefer_const_constructors.html http://dart-lang.github.io/linter/lints/prefer_const_constructors.html
- http://dart-lang.github.io/linter/lints/prefer_const_declarations.html http://dart-lang.github.io/linter/lints/prefer_const_declarations.html
- http://dart-lang.github.io/linter/lints/prefer_const_literals_to_create_immutables.html http://dart-lang.github.io/linter/lints/prefer_const_literals_to_create_immutables.html
或者当你使用时提醒你const
但无论如何可以推断
- http://dart-lang.github.io/linter/lints/unnecessary_const.html http://dart-lang.github.io/linter/lints/unnecessary_const.html
也可以看看https://www.dartlang.org/guides/language/analysis-options https://www.dartlang.org/guides/language/analysis-options
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)