问题描述
在flutter
中,任何人都可以通过示例清楚地解释我对key
感到困惑,代码如下
In flutter
anyone explain clearly with examplemy confusion about key
, code as below
MyHomepage({Key key, this.title}) : super(key: key);
推荐答案
代码是MyHomepage
小部件的构造函数.
The code is the constructor of the MyHomepage
widget.
{Key key, this.title}
声明2个可选的命名参数(由于{}
而命名为可选参数),其中
declares 2 optional named parameters (optional named because of {}
) where
-
第一个名称为
key
,类型为Key`
the first is of name
key
with typeKey`
第二个对象的名称为title
,其字段类型为this.title
,并使用传递的值自动初始化this.title
这是一个很好的语法糖,可以节省一些写作.
the 2nd is of name title
with the type of the field this.title
and automatically initializes this.title
with the passed valueThis is nice syntactic sugar that saves some writing.
:
启动初始化程序列表.初始化列表允许某些人在将调用转发给超类的构造函数之前执行一些表达式.
:
starts the initializer list.The initializer list allows some to execute some expressions before the call is forwarded to the constructor of the super class.
初始化类时,将禁止对this
的读取访问,直到对超级构造函数的调用完成为止(直到构造函数的主体执行-在您的示例中,构造函数没有主体).
When a class is initialized, read access to this
is forbidden until the call to the super constructor is completed (until the body of the constructor is executed - in your example the constructor has no body).
初始化程序列表通常用于通过assert(key != null)
验证传递的参数值或使用计算值初始化final
字段(final
字段以后无法初始化或更新).
The initializer list is often use to validate passed parameter values with assert(key != null)
or to initialize final
fields with calculated values (final
fields can't be initialized or updated later).
super(key: key)
转发到超类的构造函数,并将传递给MyHomepage
的参数key
传递给超构造函数key
参数(与MyHomepage({Key key})
相同).
super(key: key)
forwards to the constructor of the super class and passes the parameter key
passed to MyHomepage
to the super constructors key
parameter (same as for MyHomepage({Key key})
).
这篇关于颤抖MyhomePage({Key key,this.title}):super(key:key);请任何人以颤振的例子清楚地说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!