简体   繁体   English

如果一个小部件声明为 const,它是否会传播给它的孩子?

[英]If a widget declared const, does it spread to its child?

Are there any practical difference between these two?这两者之间有什么实际区别吗?

If a widget declared const, does it spread to it child?如果一个小部件声明为 const,它会传播给它的孩子吗?

Container(
    height: tabHeight,
    child: const Tab(
      icon: Icon(Icons.settings),
    ),
),

vs对比

Container(
    height: tabHeight,
    child: const Tab(
      icon: const Icon(Icons.settings),
    ),
),

Yes The const keyword is optional when obvious.const关键字在显而易见时是可选的。

For example, we don't have to write:例如,我们不必写:

const example = const Something(const Other());

We can just write:我们可以写:

const example = Something(Other());

You can omit the const keyword when initialising expressions of a const declaration.在初始化const声明的表达式时,可以省略const关键字。 Below is what the official documentation says:以下是官方文档所说的:

The const keyword isn't just for declaring constant variables. const 关键字不仅仅用于声明常量变量。 You can also use it to create constant values, as well as to declare constructors that create constant values.您还可以使用它来创建常量值,以及声明创建常量值的构造函数。 Any variable can have a constant value.任何变量都可以有一个常数值。

var foo = const []; You can omit const from the initializing expression of a const declaration.您可以在 const 声明的初始化表达式中省略 const。 For details, see DON'T use const redundantly.有关详细信息,请参阅不要冗余使用 const。

foo = [1, 2, 3]; // Was const []

I hope this helps.我希望这有帮助。

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

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