【问题标题】:Common Flutter Error (missing required argument)常见的颤振错误(缺少必需的参数)
【发布时间】:2021-10-26 20:09:33
【问题描述】:

我是 Flutter 的新手,我正在尝试测试一些库。 但不幸的是,当我运行这段代码时,我得到了错误:

错误信息:

error G4A9793AD: The value 'null' can't be assigned to the parameter type 'Key' because 'Key' is not nullable.

error image url

...some code...
    Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: MainPage(title: 'Flutter Convex BottomBar Sample'),
        );
      }
    }
 
class MainPage extends StatefulWidget {
  MainPage({required Key key, required this.title}) : super(key: key);
...some code...

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    您不能将null 的值传递给key 参数,因为它不可为空(不能将null 作为值)将其标记为可空,您应该添加?

    MainPage({required Key? key, required this.title}) : super(key: key);
    

    无论如何,我认为这不是您想要的,因为它也是不允许的。只需删除 required 关键字或将有效密钥传递给它:

    MainPage({Key key, required this.title}) : super(key: key);
    

    【讨论】:

      【解决方案2】:

      要么从MainPage 中删除Key key 变量,方法是删除它,

      或者只提供一个密钥

      home: MainPage(title: 'Flutter Convex BottomBar Sample',key : Key('homepage'),),
      

      【讨论】:

        猜你喜欢
        • 2022-01-01
        • 2021-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多