简体   繁体   English

在 Dart 中,'const' 参数有什么区别?

[英]In Dart, what's the difference between 'const' parameter?

padding: const EdgeInsets.all(25.0)
padding: EdgeInsets.all(25.0),

In Dart demo, most of padding or child add const, is there any optimization? Dart demo中,大部分padding或者child加const,有什么优化吗?

https://docs.flutter.io/flutter/widgets/Padding-class.html https://docs.flutter.io/flutter/widgets/Padding-class.html

Let us consider we have these three lines of code:让我们考虑一下我们有这三行代码:

1.const EdgeInsets.all(25.0)
2.const EdgeInsets.all(25.0)
3.const EdgeInsets.all(25.0)

1. At first line EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget and then it creates a constant object with same value for rendering if it is found in another place. 1. 在第一行 EdgeInsets 类创建一个新对象并为其分配左、上、右、下的值并渲染给定的小部件,然后创建一个具有相同值的常量对象进行渲染,如果它在另一个地方找到。

2. Hey there is already an object with this value, so just render it. 2. 嘿,已经有一个具有这个值的对象,所以渲染它。

3. Hey there is already an object with this value, so just render it. 3. 嘿,已经有一个具有这个值的对象,所以渲染它。

Now, let us consider these scenario:现在,让我们考虑这些场景:

1.EdgeInsets.all(25.0)
2.EdgeInsets.all(25.0)
3.EdgeInsets.all(25.0)

1. At first line, EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget. 1. 在第一行,EdgeInsets 类创建一个新对象并为其分配左、上、右、下的值,并渲染给定的小部件。

2. At the second line, EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget. 2. 在第二行,EdgeInsets 类创建一个新对象并为其分配左、上、右、下的值并呈现给定的小部件。

3. At third line, EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget. 3. 在第三行,EdgeInsets 类创建一个新对象并为左、上、右、下分配其值并呈现给定的小部件。

So, by using const we can reduce the time for recreating the same object every time and using it, instead, we create an object once and then reuse it at every time we need.因此,通过使用 const,我们可以减少每次重新创建同一个对象并使用它的时间,相反,我们创建一个对象,然后在每次需要时重用它。

"const" has a meaning that's a bit more complex and subtle in Dart. “const”在 Dart 中的含义更为复杂和微妙。 const modifies values . const 修改 You can use it when creating collections, like const [1, 2, 3], and when constructing objects (instead of new) like const Point(2, 3).您可以在创建集合时使用它,例如 const [1, 2, 3],以及在构造对象(而不是 new)时,例如 const Point(2, 3)。 Here, const means that the object's entire deep state can be determined entirely at compile time and that the object will be frozen and completely immutable.在这里,const 意味着对象的整个深度状态可以在编译时完全确定,并且对象将被冻结并且完全不可变。

Const objects have a couple of interesting properties and restrictions: They must be created from data that can be calculated at compile time. Const 对象有几个有趣的属性和限制:它们必须从可以在编译时计算的数据创建。 A const object does not have access to anything you would need to calculate at runtime. const 对象无权访问您需要在运行时计算的任何内容。 1 + 2 is a valid const expression, but new DateTime.now() is not. 1 + 2 是一个有效的常量表达式,但 new DateTime.now() 不是。 They are deeply, transitively immutable.它们是深刻的、可传递的不可变的。 If you have a final field containing a collection, that collection can still be mutable.如果您有一个包含集合的最终字段,该集合仍然可以是可变的。 If you have a const collection, everything in it must also be const, recursively.如果您有一个 const 集合,则其中的所有内容也必须递归地为 const。 They are canonicalized .它们是规范化的 This is sort of like string interning: for any given const value, a single const object will be created and re-used no matter how many times the const expression(s) are evaluated.这有点像字符串实习:对于任何给定的 const 值,无论对 const 表达式求值多少次,都将创建并重用单个 const 对象。 In other words:换句话说:

Src here 源代码在这里

const means that the object's (padding here) entire deep state can be determined entirely at compile time and that the padding will be frozen and completely immutable. const意味着对象的(这里的填充)整个深度状态可以在编译时完全确定,填充将被冻结并且完全不可变。

PS: Please read comment for better explanation PS:请阅读评论以获得更好的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM