小编DVC*_*one的帖子

如何在自定义小部件构造函数中添加和使用密钥

我收到通知警告(不是错误),假设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)

解决警告通知?

constructor dart flutter flutter-widget

5
推荐指数
1
解决办法
6104
查看次数

标签 统计

constructor ×1

dart ×1

flutter ×1

flutter-widget ×1