【问题标题】:Is there a solution to the error: The parameter 'uid' can't have a value of 'null' because of its type, but the implicit default value is 'null'错误有没有解决办法:参数'uid'不能有'null'的值,因为它的类型,但是隐含的默认值是'null'
【发布时间】:2021-08-21 07:45:39
【问题描述】:

有没有解决错误的办法:参数'uid'不能有'null'的值,因为它的类型,但是隐含的默认值是'null'。

我的代码:

class UserModel {
  final String uid;
  UserModel({this.uid});
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    这是因为null safety feature。 你可以这样做:

    为您的参数添加默认值

    class UserModel {
      final String uid;
      UserModel({this.uid = ''});
    }
    

    或定义参数可为空

    class UserModel {
      final String? uid;
      UserModel({this.uid});
    }
    

    【讨论】:

    • 谢谢,如果我使用以下命令“flutter run --no-sound-null-safety”,我还需要进行此更改吗?
    • 如果在 pubspec.yaml 你有这个约束,你的代码是空安全的。 environment: sdk: ">=2.12.0 <3.0.0"。如果你想禁用空安全更改它environment: sdk: ">=2.7.0 <3.0.0"
    • 你也可以把参数设为必选`UserModel({required this.uid });`
    猜你喜欢
    • 2021-06-24
    • 2021-09-05
    • 2022-06-14
    • 2021-10-28
    • 2021-11-12
    • 2022-01-03
    • 2022-01-20
    • 1970-01-01
    • 2022-08-13
    相关资源
    最近更新 更多