【问题标题】:Flutter-The parameter 'email' can't have a value of 'null' because of its type, but the implicit default value is 'null'Flutter-参数\'email\'由于其类型不能取值为\'null\',但隐式默认值为\'null\'
【发布时间】:2023-02-09 20:08:29
【问题描述】:

我是 flutter 的新手,我正在通过创建模型来处理 http post 请求。

class LoginResponseModel {
  final String token;
  final String error;

  LoginResponseModel({this.token, this.error});

  factory LoginResponseModel.fromJson(Map<String, dynamic> json) {
    return LoginResponseModel(
      token: json["token"] != null ? json["token"] : "",
      error: json["error"] != null ? json["error"] : "",
    );
  }
}

class LoginRequestModel {
  String email;
  String password;
  String tenant;

  LoginRequestModel({
    this.email,
    this.password,
    this.tenant,
  });

  Map<String, dynamic> toJson() {
    Map<String, dynamic> map = {
      'email': email.trim(),
      'password': password.trim(),
      'token':tenant.trim(),
    };

    return map;
  }
}

我在这段代码的以下部分遇到错误:

LoginRequestModel({
    this.email,
    this.password,
    this.tenant,
  });

由于其类型,参数“email”的值不能为“null”,但隐式默认值为“null”

【问题讨论】:

    标签: flutter httprequest


    【解决方案1】:

    对于命名参数,如果您不指明它是required,那么它可能是null

    将其更改为:

    LoginRequestModel({
        required this.email,
        required this.password,
        required this.tenant,
      });
    

    【讨论】:

      猜你喜欢
      • 2021-08-19
      • 2022-08-13
      • 2021-06-24
      • 2021-10-28
      • 2021-11-12
      • 2022-01-03
      • 2022-01-20
      • 1970-01-01
      • 2022-06-14
      相关资源
      最近更新 更多