Flutter 无法为具有非最终字段的类定义常量构造函数
我正在尝试使用flutter呈现一个小部件,但出现以下错误: “无法为具有非最终字段的类定义常量构造函数” “常量构造函数无法调用状态的非常量超级构造函数” “未定义名称参数'Key'ins” 出现此错误的代码如下所示:Flutter 无法为具有非最终字段的类定义常量构造函数,flutter,Flutter,我正在尝试使用flutter呈现一个小部件,但出现以下错误: “无法为具有非最终字段的类定义常量构造函数” “常量构造函数无法调用状态的非常量超级构造函数” “未定义名称参数'Key'ins” 出现此错误的代码如下所示: class ContainerButton extends StatefulWidget { @override ContainerButtonState createState() => ContainerButtonState(); } class Cont
class ContainerButton extends StatefulWidget {
@override
ContainerButtonState createState() => ContainerButtonState();
}
class ContainerButtonState extends State<ContainerButton> {
final ButtonType buttonType;
const CustomButton({Key key, this.buttonType}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(21),
color: Color(0xfff4f5f9),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Flexible(
child: CustomButton(buttonType: ButtonType.download),
),
Flexible(
child: CustomButton(buttonType: ButtonType.share),
),
Flexible(
child: CustomButton(buttonType: ButtonType.problem),
),
],
),
);
}
}
class ContainerButton扩展StatefulWidget{
@凌驾
ContainerButtonState createState()=>ContainerButtonState();
}
类ContainerButtonState扩展状态{
最终按钮类型按钮类型;
constCustomButton({Key-Key,this.buttonType}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
返回容器(
填充:边缘设置。全部(21),
颜色:颜色(0xFFF4F9),
孩子:排(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
灵活的(
子项:CustomButton(buttonType:buttonType.download),
),
灵活的(
子项:CustomButton(buttonType:buttonType.share),
),
灵活的(
子项:CustomButton(buttonType:buttonType.problem),
),
],
),
);
}
}
如果有任何提示,我将不胜感激。谢谢,
无法为具有非最终字段的类定义常量构造函数
const
构造函数的使用使得类属性不会改变。如果不添加final
,则允许在声明后更改该变量
常量构造函数无法调用状态的非常量超级构造函数
在这种情况下,类状态
没有常量
构造函数(请参阅)。扩展没有const
构造函数的类不允许扩展它的类具有const
构造函数
当谈到Dart中的继承时,您只能使扩展类的限制更少,而不是更多。如果要生成一个const
构造函数,则会使类ContainerButtonState
比State
类更具限制性。如果您想了解更多有关Dart继承的信息,请点击这里
我希望我已经清楚了 根据错误,只需从构造函数中删除
const
关键字即可。以下代码应删除该错误:
class ContainerButtonState extends State<ContainerButton> {
final ButtonType buttonType;
CustomButton({Key key, this.buttonType}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(21),
color: Color(0xfff4f5f9),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Flexible(
child: CustomButton(buttonType: ButtonType.download),
),
Flexible(
child: CustomButton(buttonType: ButtonType.share),
),
Flexible(
child: CustomButton(buttonType: ButtonType.problem),
),
],
),
);
}
}
class ContainerButtonState扩展状态{
最终按钮类型按钮类型;
CustomButton({Key-Key,this.buttonType}):超级(Key:Key);
@凌驾
小部件构建(构建上下文){
返回容器(
填充:边缘设置。全部(21),
颜色:颜色(0xFFF4F9),
孩子:排(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
灵活的(
子项:CustomButton(buttonType:buttonType.download),
),
灵活的(
子项:CustomButton(buttonType:buttonType.share),
),
灵活的(
子项:CustomButton(buttonType:buttonType.problem),
),
],
),
);
}
}
将常量CustomButton({Key Key,this.buttonype})
替换为常量ContainerButton({Key,this.buttonype})
并且必须将其放置在statefull类之外
以下是最终代码:
class ContainerButton extends StatefulWidget {
final ButtonType buttonType;
const ContainerButton({Key key, this.buttonType}) : super(key: key);
@override
ContainerButtonState createState() => ContainerButtonState();
}
class ContainerButtonState extends State<ContainerButton> {
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(21),
color: Color(0xfff4f5f9),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Flexible(
child: CustomButton(buttonType: ButtonType.download),
),
Flexible(
child: CustomButton(buttonType: ButtonType.share),
),
Flexible(
child: CustomButton(buttonType: ButtonType.problem),
),
],
),
);
}
}
class ContainerButton扩展StatefulWidget{
最终按钮类型按钮类型;
const ContainerButton({Key-Key,this.buttonType}):super(Key:Key);
@凌驾
ContainerButtonState createState()=>ContainerButtonState();
}
类ContainerButtonState扩展状态{
@凌驾
小部件构建(构建上下文){
返回容器(
填充:边缘设置。全部(21),
颜色:颜色(0xFFF4F9),
孩子:排(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
灵活的(
子项:CustomButton(buttonType:buttonType.download),
),
灵活的(
子项:CustomButton(buttonType:buttonType.share),
),
灵活的(
子项:CustomButton(buttonType:buttonType.problem),
),
],
),
);
}
}
谢谢,伙计。其他答案都没有解释这个话题。我在寻找解释。