在小部件树中使用 const 会提高性能吗? [英] Does using const in the widget tree improve performance?
问题描述
创建widget树时,在静态widget之前插入const
会提高性能吗?
When creating a widget tree, will inserting const
before static widgets improve performance?
即
child: const Text('This is some text');
对比
child: Text('This is some text');
我知道,在 Dart 2 中,const
是可选的,并且会在某些地方自动插入.这是其中一种情况吗?如果不是,使用 const
会减少内存使用量/提高性能吗?
I know that, with Dart 2, const
is optional and will be inserted automatically is some places. Is this one of those situations? If it isn't, will using const
reduce memory usage/improve performance?
感谢您的回答!
推荐答案
这是一个很小的性能改进,但它可以在较大的应用程序或经常重建视图的应用程序中累积,例如由于动画.const
减少了垃圾收集器所需的工作.
It is a small performance improvement, but it can add up in larger apps or apps where the view is rebuilt often for example because of animations.
const
reduces the required work for the Garbage Collector.
您可以在 analysis_options.yaml
中启用一些 linter 规则,告诉您何时应该添加 const
因为它不是推断的,但可能像
You can enable some linter rules in analysis_options.yaml
that tell you when you should add const
because it's not inferred but would be possible like
- 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_literals_to_create_immutables.html
或者在您使用 const
时提醒您但它仍然是推断的
or that reminds you when you use const
but it is inferred anyway
另见https://www.dartlang.org/guides/language/analysis-选项
这篇关于在小部件树中使用 const 会提高性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!