在小部件树中使用 const 会提高性能吗? [英] Does using const in the widget tree improve performance?

查看:27
本文介绍了在小部件树中使用 const 会提高性能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建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

或者在您使用 const 时提醒您但它仍然是推断的

or that reminds you when you use const but it is inferred anyway

另见https://www.dartlang.org/guides/language/analysis-选项

这篇关于在小部件树中使用 const 会提高性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆