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;
}
}
flutter 1.升级2.X在模型类中序列化JSON报错Non-nullable instance field 'title' must be initialized.的更多相关文章
- .net winform 调用类中的webbrowser 报错:当前线程不在单线程单元中,因此无法实例化 ActiveX
遇到这个恶心的问题纠缠得不要不要的,大家遇到了的话希望不要走弯路,经过这个折腾让我有点怀疑人生了.哈哈哈 解决代码如下: //插入一个新线程用于处理验证码 Thread thd = new Threa ...
- python---Django中模型类中Meta元对象了解
Django中模型类中Meta元对象了解 1.使用python manage.py shell 进入编辑命令行模式,可以直接进入项目(为我们配置好了环境) python manage.py shell ...
- linux下启动dbca或netmgr类的图形界面报错解决
linux下启动dbca或netmgr类的图形界面报错解决 Xlib: connection to ":0.0" refused by server Xlib: No pro ...
- Spring MVC普通类或工具类中调用service报空空指针的解决办法(调用service报java.lang.NullPointerException)
当我们在非Controller类中应用service的方法是会报空指针,如图: 这是因为Spring MVC普通类或工具类中调用service报空null的解决办法(调用service报java.la ...
- Springboot实体类转JSON报错Could not find acceptable representation & 设置访问项目根路径的默认欢迎页面
=================实体类转JSON报错的解决办法============= 之前在springmvc的时候也报过这个错,原因以及springmvc中解决办法参考:https://www ...
- 将Xcode升级到10.0以上版本,Appium启动报错的问题
前言 现在的Xcode最新版本都是在10.1,原先使用的版本是9.4.1!结果今天手贱将其升级... 然后,跑IOS自动化时,出现“Xcode version '0.1'. Support for X ...
- idea生成类中序列化id
RPC接口中要传输的对象需要序列化,需要生成序列id,idea中生成序列id的方式如下 在需要添加序列id的类中,选中类名,alt+enter就可以实现了
- 处理flutter http请求添加application/json报错Cannot set the body fields of a Request with content-type “application/json”
在flutter中在http请求发送时设置"content-type": "application/json"会出现报错Cannot set the body ...
- moviepy音视频剪辑VideoClip类fl_image方法image_func报错ValueError: assignment destination is read-only解决办法
☞ ░ 前往老猿Python博文目录 ░ moviepy音视频剪辑模块的视频剪辑基类VideoClip的fl_image方法用于进行对剪辑帧数据进行变换. 调用语法:fl_image(self, im ...
随机推荐
- 知识图谱顶会论文(ACL-2022) PKGC:预训练模型是否有利于KGC?可靠的评估和合理的方法
PKGC:预训练模型是否有利于KGC?可靠的评估和合理的方法 论文地址:Do Pre-trained Models Benefit Knowledge Graph Completion? A Reli ...
- 人生苦短,我用 python 之入门篇
Python 是一种跨平台的,开源的,免费的,解释型的高级编程语言,它具有丰富和强大的库,其应用领域也非常广泛,在 web 编程/图形处理/黑客编程/大数据处理/网络爬虫和科学计算等领域都能找到其身影 ...
- 图扑软件 3D 组态编辑器,低代码零代码构建数字孪生工厂
行业背景 随着中国制造 2025 计划的提出,新一轮的工业改革拉开序幕.大数据积累的指数级增长为智能商业爆发奠定了良好的基础,传统制造业高污染.高能耗.低效率的生产模式已不符合现代工业要求. 图扑拖拽 ...
- C#使用正则表达式来验证是否是16进制字符串
/// <summary> /// 判断是否为16进制字符串 /// </summary> /// <param name="hexString"&g ...
- 2022春每日一题:Day 16
题目:不同子串个数 这题需要利用后缀数组求出的height的性质,我们发现对于每个后缀,他的height后的所有子串就是算在答案里,因此答案只需要求出n-height[i]-sa[i]+1的和就可以了 ...
- KubeEdge快速上手与社区贡献实践
1.KubeEdge的架构特点与优势 持久化 云端组件,EdgeController,设备抽象API,CSI Driver,Admission WebHook 边缘组件,EdgeHub,MetaMan ...
- lvm+xfs的扩缩容
ext4文件系统可以经行扩缩容操作,但xfs的文件系统只能扩容,无法缩容 所以如果需要进行xfs的缩容,可以先使用xfsdump备份文件系统,然后对逻辑卷(/分区)进行缩容操作(此时原xfs文件系统会 ...
- 基于python的数学建模---图论模型(Floyd)
import numpy as np inf = 99999 # 不连通值 mtx_graph = [[0, 1, inf, 3, inf, inf, inf, inf, inf], [1, 0, 5 ...
- phpexcel 上传
<?php require_once(ROOTPATH . "inc/PHPExcel/PHPExcel.class.php");//PHPExcel//获取数据 $objP ...
- 【Devexpress】pivotGridControl设置不显示展开折叠按钮
只需要设置.效果看图二