我试图用flutter呈现一个小部件,但我得到了以下错误:
"不能为具有非最终字段的类定义const构造函数">
"常量构造函数不能调用状态"的非常量超构造函数;
"名称参数'Key'未定义">
出现此错误的代码如下:
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),
),
],
),
);
}
}
如果有任何提示,我将不胜感激。谢谢你,
根据错误,只需从Constructor中删除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),
),
],
),
);
}
}
不能为具有非最终字段的类定义const构造函数
const
构造函数用于使类属性不发生更改。通过不添加final
,您就允许该变量在声明后发生更改。
常量构造函数不能调用状态的非常量超构造函数
在这种情况下,类State
没有const
构造函数(请参阅api(。扩展一个没有const
构造函数的类不允许扩展它的类具有const
构造函数。
在Dart中讨论继承时,只能减少扩展类的限制,但不能增加限制。如果要创建const
构造函数,则会使类ContainerButtonState
比State<T>
更具限制性。如果你想了解更多关于Dart继承的信息,这里有api。
我希望我已经清楚了!
-----快速解决方案----
删除";const";中的关键字
const CustomButton({Key key, this.buttonType}) : super(key: key);
所以这看起来像
CustomButton({Key key, this.buttonType}) : super(key: key);
----详细答案----
让我们把这个问题分解一下
const constructor
表示您无法更改其值的构造函数
final
表示单个赋值:最终变量或字段必须具有初始值设定项。一旦分配了一个值,最终变量的值就不能更改
non final fields
是您将来想要更改的内容。就像你的柜台(增加一个或任何你想要的(。
代码中的问题就在这里
const CustomButton({Key key, this.buttonType}) : super(key: key);
删除";const";中的关键字
const CustomButton({Key key, this.buttonType}) : super(key: key);
修改代码
class ContainerButton extends StatefulWidget {
@override
ContainerButtonState createState() => ContainerButtonState();
}
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),
),
],
),
);
}
}
用const ContainerButton({Key key, this.buttonType})
替换const CustomButton({Key key, this.buttonType})
,并且必须将其放置在状态完整类之外
这是最后的代码:
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),
),
],
),
);
}
}