有人可以解释这个Dart语法

Sir*_*dib 12 syntax constructor dart

class TapboxA extends StatefulWidget {

 TapboxA({Key key}) : super(key: key);

}
Run Code Online (Sandbox Code Playgroud)

这部分 :

TapboxA({Key key}) 
Run Code Online (Sandbox Code Playgroud)

下半场

super(key: key);
Run Code Online (Sandbox Code Playgroud)

我理解,(对超类构造函数的调用).

但是什么是

{Key key}
Run Code Online (Sandbox Code Playgroud)

语法呢?

RX *_*abz 6

在Dart构造函数(和其他方法)中可以具有可选的命名参数:

MyClass({String namedParam}){//...}
Run Code Online (Sandbox Code Playgroud)

对于Flutter小部件构造函数:

TapboxA({Key key})  // TapboxA constructor defines a named parameters `key`
: super(key: key); //which is used within the super constructor call (which also has `key` as named parameter )
Run Code Online (Sandbox Code Playgroud)

您可以在Dart语言浏览中找到有关可选命名参数的更多信息。

  • @SirPaulMuaddib键用于对小部件重建进行细粒度控制。在无限滚动列表中,您可以通过为其项分配一个键来避免整个列表的完全重建,因为新项被添加到列表中,除非其键及其runtimType匹配,否则不会重建以前的项:https ://flutter.io/widgets-intro/#keys (2认同)