无法为具有非 Final 字段的类定义 const 构造函数 Flutter

dos*_*res 17 flutter

我正在尝试使用 flutter 渲染小部件,但收到以下错误:

“无法为具有非最终字段的类定义 const 构造函数”

“常量构造函数不能调用 State 的非常量超级构造函数”

“名称参数‘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),
          ),
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

我将不胜感激任何提示。谢谢你,

Jay*_*gar 14

根据错误,只需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),
          ),
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 例如,如果我想在主页小部件(主类)home: Login() 中使用此小部件该怎么办?错误将是不调用需要 const 表达式的非“const”构造函数。尝试使用“const”的构造函数或工厂 (2认同)

小智 9

无法为具有非 Final 字段的类定义 const 构造函数

const使用构造函数是为了保证类属性不会改变。通过不添加,final您将允许该变量在声明后更改。

常量构造函数不能调用 State 的非常量超级构造函数

在这种情况下,该类State没有const构造函数(请参阅api)。扩展没有const构造函数的类不允许扩展它的类具有const构造函数。

当谈论 Dart 中的继承时,你只能减少扩展类的限制,而不能增加限制。如果你要创建一个const构造函数,你将使类比类ContainerButtonState更具限制性State<T>。如果您想了解有关 Dart 继承的更多信息,这里有api

我希望我已经说清楚了!


小智 6

----- 快速解决方案 -----

从中删除“const”关键字

const CustomButton({Key key, this.buttonType}) : super(key: key);
Run Code Online (Sandbox Code Playgroud)

所以这看起来像

CustomButton({Key key, this.buttonType}) : super(key: key);
Run Code Online (Sandbox Code Playgroud)

----详细解答-----

让我们分解一下这个问题

const constructor表示您无法更改其值的构造函数

final表示单赋值:最终变量或字段必须有一个初始值设定项。一旦赋值,final 变量的值就不能更改

non final fields是您将来想要改变的事情。就像你的计数器(增加一或任何你想要的)。

您的代码中的问题就在这里

const CustomButton({Key key, this.buttonType}) : super(key: key);

从中删除“const”关键字

const CustomButton({Key key, this.buttonType}) : super(key: key);
Run Code Online (Sandbox Code Playgroud)

修改后的代码

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),
          ),
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 4

替换const CustomButton({Key key, this.buttonType})const ContainerButton({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),
            ),
          ],
        ),
      );
    }
  }
Run Code Online (Sandbox Code Playgroud)