flutter 1.升级2.X在模型类中序列化JSON报错Non-nullable instance field 'title' must be initialized.

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了flutter 1.升级2.X在模型类中序列化JSON报错Non-nullable instance field 'title' must be initialized.脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

flutter 1.升级2.X在模型类中序列化JSON报错

Non-nullable instance field 'tITle' must be initialized. 
Try adding an initializer exPression, 
or add a field initializer in this constructor, or mark it 'late'.

修改方案--添加late

flutter 1.升级2.X在模型类中序列化JSON报错Non-nullable instance field 'title' must be initialized.

flutter 1.升级2.X在模型类中序列化JSON报错Non-nullable instance field 'title' must be initialized.

原有代码

class Autogenerated {
  List<Result> result;

  Autogenerated({this.result});

  Autogenerated.FromJson(Map<String, dynamic> json) {
    if (json['result'] != null) {
      result = new List<Result>();
      json['result'].foreach((v) {
        result.add(new Result.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.result != null) {
      data['result'] = this.result.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Result {
  String sId;
  String title;
  String status;
  String pic;
  String url;

  Result({this.sId, this.title, this.status, this.pic, this.url});

  Result.fromJson(Map<String, dynamic> json) {
    sId = json['_id'];
    title = json['title'];
    status = json['status'];
    pic = json['pic'];
    url = json['url'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['_id'] = this.sId;
    data['title'] = this.title;
    data['status'] = this.status;
    data['pic'] = this.pic;
    data['url'] = this.url;
    return data;
  }
}

修改后的

class Autogenerated {
  late List<Result> result;

  Autogenerated({required this.result});

  Autogenerated.fromJson(Map<String, dynamic> json) {
    if (json['result'] != null) {
      result = [];
      json['result'].forEach((v) {
        result.add(new Result.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.result != null) {
      data['result'] = this.result.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Result {
  late String sId;
  late String title;
  late String status;
  late String pic;
  late String url;

  Result(
      {required this.sId,
      required this.title,
      required this.status,
      required this.pic,
      required this.url});

  Result.fromJson(Map<String, dynamic> json) {
    sId = json['_id'];
    title = json['title'];
    status = json['status'];
    pic = json['pic'];
    url = json['url'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['_id'] = this.sId;
    data['title'] = this.title;
    data['status'] = this.status;
    data['pic'] = this.pic;
    data['url'] = this.url;
    return data;
  }
}

脚本宝典总结

以上是脚本宝典为你收集整理的flutter 1.升级2.X在模型类中序列化JSON报错Non-nullable instance field 'title' must be initialized.全部内容,希望文章能够帮你解决flutter 1.升级2.X在模型类中序列化JSON报错Non-nullable instance field 'title' must be initialized.所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。