DVC*_*one 5 constructor dart flutter flutter-widget
我收到通知警告(不是错误),假设Use key in widget constructors.我有这样的无状态类:
class TeaTile extends StatelessWidget {
final TheTea? tea;
const TeaTile({this.tea}); //the warning in hire!
@override
Widget build(BuildContext context) {
return Container();
}
}
Run Code Online (Sandbox Code Playgroud)
基本的无状态格式有一个像这样的键:
class TeaTile extends StatelessWidget {
const TeaTile({ Key? key }) : super(key: key); //this one
@override
Widget build(BuildContext context) {
return Container();
}
}
Run Code Online (Sandbox Code Playgroud)
我知道如何禁用关键规则use_key_in_widget_constructors: false。但我不想这样做。那么,我如何key添加
final TheTea? tea;
const TeaTile({this.tea});
Run Code Online (Sandbox Code Playgroud)
解决警告通知?
使用超级初始化器更新 Dart 2.17:
final TheTea? tea;
const TeaTile({ super.key, this.tea });
Run Code Online (Sandbox Code Playgroud)
构造函数中的关键字super是以下方法的快捷方式。
较旧的 Dart 版本:
final TheTea? tea;
const TeaTile({ Key? key, this.tea }) : super(key: key);
Run Code Online (Sandbox Code Playgroud)
基本上是两者的组合,您仍然采用一个命名参数key,它将其值传递给超级构造函数,以及另一个命名参数tea,它将设置您的最终变量值。
| 归档时间: |
|
| 查看次数: |
6104 次 |
| 最近记录: |