[英]How to Add and Use key in Custom widget constructors
I got notification warning (Not Error) about Use key in widget constructors.
我收到了关于
Use key in widget constructors.
通知警告(非错误) Use key in widget constructors.
let say I have stateless class like this :假设我有这样的无状态类:
class TeaTile extends StatelessWidget {
final TheTea? tea;
const TeaTile({this.tea}); //the warning in hire!
@override
Widget build(BuildContext context) {
return Container();
}
}
the basic stateless format has a key like this :基本的无状态格式有一个这样的键:
class TeaTile extends StatelessWidget {
const TeaTile({ Key? key }) : super(key: key); //this one
@override
Widget build(BuildContext context) {
return Container();
}
}
I know how to disable the key rule use_key_in_widget_constructors: false
.我知道如何禁用关键规则
use_key_in_widget_constructors: false
。 but I don't want to do it.但我不想这样做。 so, how I add
key
in所以,我如何添加
key
final TheTea? tea;
const TeaTile({this.tea});
to solve the warning notification?解决警告通知?
You can just use:你可以只使用:
final TheTea? tea;
const TeaTile({ Key? key, this.tea }) : super(key: key);
Basically a combination of both, you're still taking a named parameter key
, that will pass it's value to the super constructor, and another named parameter tea
that would set your final variable value.基本上是两者的结合,你仍然使用一个命名参数
key
,它将它的值传递给超级构造函数,另一个命名参数tea
将设置你的最终变量值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.