【问题标题】:cant define a const constructor for a class with non final fields Flutter无法为具有非最终字段的类定义 const 构造函数 Flutter
【发布时间】:2020-08-20 10:18:30
【问题描述】:

我正在尝试使用颤振渲染小部件,但出现以下错误:

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

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

“名称参数‘键’未定义”

出现此错误的代码如下:

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),
          ),
        ],
      ),
    );
  }
}

我会很感激任何提示。谢谢,

【问题讨论】:

    标签: flutter


    【解决方案1】:

    根据错误,只需从构造函数中删除 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),
              ),
            ],
          ),
        );
      }
    }
    

    【讨论】:

    • 如果我想在 home 小部件(主类)home 中使用这个小部件怎么办:例如 Login()。错误将是 annot 在需要 const 表达式的地方调用非“const”构造函数。尝试使用'const'的构造函数或工厂
    【解决方案2】:

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

    使用const 构造函数,以便类属性不会改变。通过不添加final,您允许该变量在声明后更改。

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

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

    当谈到 Dart 中的继承时,您只能减少对扩展类的限制,但不能增加限制。如果你要创建一个const 构造函数,你将使ContainerButtonState 类比State&lt;T&gt; 类更具限制性。如果你想了解更多关于 Dart 的继承,这里有api

    我希望我已经清楚了!

    【讨论】:

    • 谢谢,伙计。没有其他答案解释这个主题。我正在寻找解释。
    【解决方案3】:

    const CustomButton({Key key, this.buttonType}) 替换为 const ContainerButton({Key key, this.buttonType}) 并且你必须将它放在 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),
                ),
              ],
            ),
          );
        }
      }
    

    【讨论】:

      【解决方案4】:

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

      中删除“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),
                ),
              ],
            ),
          );
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-26
        • 1970-01-01
        • 2018-02-18
        • 1970-01-01
        • 2021-07-24
        • 1970-01-01
        • 1970-01-01
        • 2015-01-22
        相关资源
        最近更新 更多