包含 List<Object> 的对象的映射用于 sqlite [英] Map of object containing a List<Object> for sqlite
问题描述
我正在设置我的模型类以确认 sqflite 的文档,其中建议包含一个命名构造函数来转换到/从 Maps 以更好地处理类和数据库之间的数据.我能找到的每个例子都很简单,类属性都是简单的数据类型.
I am setting up my model classes to confirm to the docs for sqflite which suggest including a named constructor to convert to/from Maps to better handling of data between the classes and the DB. Every example I can find is very simple, with class properties all being simple data types.
使用下面显示的构造函数和方法,在处理这样的类时,转换到/从 Map 非常简单.
Using the constructor and method shown below, converting to/from Map is quite simple when dealing with a class such as this.
class Human{
final String name;
final String height;
Final String weight;
Human({this.name, this.height, this.weight});
}
然而,当你有一个字段有点复杂的类时,我不明白如何在命名构造函数和 xxx 方法中构造事物以返回我相信"我应该得到的数据映射.
However, when you have a class where one of the fields is a bit more complex, I do not understand how to structure things within the named constructor and xxx method to return the map of data that I 'believe' I should get.
class Human{
final String name;
final String height;
Final String weight;
List<Child> children = [];
Human({this.name, this.height, this.weight, this.children});
}
Human({this.name, this.height, this.weight, this.children});
Human.fromMap(Map<String, dynamic> map)
: name = map['name'],
height = map['height'],
weight = map['weight'],
children = map['children'];
Map<String, dynamic> toMap() {
return {
'name': name,
'height': height,
'weight': weight,
'children': children,
};
}
列表孩子是我正在努力解决的部分.我相信您必须将每个 Child 对象也转换为父地图中的地图,但我在这里输了.
The List children is the part I am struggling with. I believe you have to get each Child object ALSO converted to a map within the parent map, but am losing the battle here.
我的方法在这儿吗?我应该使用其他方法来完成此操作吗?
Is my approach way off here? Is there some other method I should be using to accomplish this?
如有任何帮助,我们将不胜感激.
Any assistance would be much appreciated.
推荐答案
这里我解释一下
- 如何将模型对象转换为 Map 以与 sqlite 一起使用
- 如何将 Map 对象从 sqlite 转换为模型类.
- 如何在 Flutter 中正确解析 JSON 响应
- 如何将模型对象转换为 JSON
以上所有问题都有相同的答案.Dart 对这些操作有很大的支持.这里我会用一个详细的例子来说明.
All of the above questions has same answer. Dart has great support for these operations. Here I am going to illustrate it with a detailed example.
class DoctorList{
final List<Doctor> doctorList;
DoctorList({this.doctorList});
factory DoctorList.fromMap(Map<String, dynamic> json) {
return DoctorList(
doctorList: json['doctorList'] != null
? (json['doctorList'] as List).map((i) => Doctor.fromJson(i)).toList()
: null,
);
}
Map<String, dynamic> toMap() {
final Map<String, dynamic> data = Map<String, dynamic>();
if (this.doctorList != null) {
data['doctorList'] = this.doctorList.map((v) => v.toMap()).toList();
}
return data;
}
}
上面的 DoctorList
类有一个成员,它包含一个 'Doctor
' 对象列表..
The above DoctorList
class has a member which holds a list of 'Doctor
' objects..
看看我是如何解析医生列表的.
And see how I parsed the doctorList.
doctorList: json['doctorList'] != null
? (json['doctorList'] as List).map((i) => Doctor.fromMap(i)).toList()
: null,
您可能想知道 Doctor
类会是什么样子.给你
You may wonder, how the Doctor
class may look like. Here you go
class Doctor {
final String doCode;
final String doctorName;
Doctor({this.doCode, this.doctorName});
factory Doctor.fromMap(Map<String, dynamic> json) {
return Doctor(
doCode: json['doCode'],
doctorName: json['doctorName'],
);
}
Map<String, dynamic> toMap() {
final Map<String, dynamic> data = Map<String, dynamic>();
data['doCode'] = this.doCode;
data['doctorName'] = this.doctorName;
return data;
}
}
<小时>
仅此而已.希望你有这个想法.干杯!
That's all. Hope you got the idea. Cheers!
这篇关于包含 List<Object> 的对象的映射用于 sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!