zoukankan      html  css  js  c++  java
  • 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

    在这里插入图片描述
    在这里插入图片描述

    原有代码

    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;
      }
    }
    
    
  • 相关阅读:
    一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——收流篇:(一)简介
    常用perl脚本工具
    一步步学习操作系统(2)——在STM32上实现一个可动态加载kernel的"my-boot"
    一步步学习操作系统(1)——参照ucos,在STM32上实现一个简单的多任务(“啰里啰嗦版”)
    一步步学习操作系统(1)——参照ucos,在STM32上实现一个简单的多任务(“精简版”)
    perl的正则表达式
    vim小技巧
    一些方便的bash命令
    Linux磁盘及文件系统管理
    Linux权限扩展
  • 原文地址:https://www.cnblogs.com/sugartang/p/15302924.html
Copyright © 2011-2022 走看看