flutter 1.升级2.X在模型类中序列化JSON报错Non-nullable instance field 'title' must be initialized.
2021/9/17 6:04:57
本文主要是介绍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.的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-19vue2 开发移动端h5 使用那个ui框架比较好?-icode9专业技术文章分享
- 2024-11-19ReactJS结合TypeScript、Vite、Redux和TanStack (React Query) 实战教程
- 2024-11-19Vue3资料入门教程:零基础快速上手指南
- 2024-11-19Vue3资料:新手入门教程与实战指南
- 2024-11-19Vue资料:新手入门vue.js教程与实践指南
- 2024-11-19Vue资料:初学者入门指南
- 2024-11-18tcpdf可以等待vue动态页面加载完成后再生成pdf吗?-icode9专业技术文章分享
- 2024-11-16Vue3资料:新手入门必读教程
- 2024-11-16Vue3资料:新手入门全面指南
- 2024-11-16Vue资料:新手入门完全指南