我有这个json:
{
"ProductCoreId":0,
"LifeCycle":1,
"OwnerPartyRoleId":0,
"TriggerImages":null,
"TriggerImagesUrl":[
"/Files/Images/Channels/sdp.png"
],
"TriggerChannelIds":[
1287
],
"ActionImages":null,
"ActionImagesUrl":[]
}
您可以看到,我有2个列表TriggerImagesUrl
和TriggerChannelIds
。现在,我想将此json模型保存到sqlite中,因此我根据json制作了该类模型以保存到数据库中:
class ToolsByChannelIdDbModel {
int productCoreId;
int lifeCycle;
int ownerPartyRoleId;
Null triggerImages;
String triggerImagesUrl;
String triggerChannelIds;
ToolsByChannelIdDbModel(
{this.productCoreId,
this.lifeCycle,
this.ownerPartyRoleId,
this.triggerImages,
this.triggerImagesUrl,
this.triggerChannelIds});
ToolsByChannelIdDbModel.fromJson(Map<String, dynamic> json) {
productCoreId = json['ProductCoreId'];
lifeCycle = json['LifeCycle'];
ownerPartyRoleId = json['OwnerPartyRoleId'];
triggerImages = json['TriggerImages'];
triggerImagesUrl = json['TriggerImagesUrl'];
triggerChannelIds = json['TriggerChannelIds'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['ProductCoreId'] = this.productCoreId;
data['LifeCycle'] = this.lifeCycle;
data['OwnerPartyRoleId'] = this.ownerPartyRoleId;
data['TriggerImages'] = this.triggerImages;
data['TriggerImagesUrl'] = this.triggerImagesUrl;
data['TriggerChannelIds'] = this.triggerChannelIds;
return data;
}
}
由于您现在sqlite不接受列表作为类型,所以我将列表作为json字符串制成,所以我这样保存:
ToolsByChannelIdDbModel model = ToolsByChannelIdDbModel();
model.productCoreId = item.productCoreId;
model.lifeCycle = item.lifeCycle;
model.ownerPartyRoleId = item.ownerPartyRoleId;
model.triggerImagesUrl = jsonEncode(item.triggerImagesUrl);
model.triggerChannelIds = jsonEncode(item.triggerChannelIds);
db.insert("tools_table", model.toJson());
item.triggerImagesUrl
和item.triggerChannelIds
是一个列表,我将它们映射到json字符串中以保存到数据库中。到目前为止,一切都很好,并且这些列表已保存到数据库中。当我进行查询并且想要将db.rawQuery
的结果解码为我的对象时,就会发生问题。
Future<List<ToolsByChannelIdDbModel>> getTools(
int channelId) async {
List<ToolsByChannelIdDbModel> list =
try {
var result = await DBProvider.db.getToolsById(channelId);
result.forEach((item) {
list.add(ToolsByChannelIdDbModel.fromJson(item));
});
} catch (e) {
print(e);
}
return list;
}
}
[await DBProvider.db.getToolsById(channelId);
部分工作正常,返回了数据映射,triggerImagesUrl
和triggerChannelIds
是json字符串,但是当我想通过此部分中的ToolsByChannelIdDbModel.fromJson(item)
将映射转换为对象时:
String triggerImagesUrl;字符串triggerChannelIds;
在模型中,我出错了,因为我将这些字段声明为String,但它们是列表。
如何将这些列表存储到数据库中以及如何将它们映射到对象中?
尝试一下:
class ToolsByChannelIdDbModel {
int productCoreId;
int lifeCycle;
int ownerPartyRoleId;
dynamic triggerImages;
List<String> triggerImagesUrl;
List<int> triggerChannelIds;
dynamic actionImages;
List<dynamic> actionImagesUrl;
/* Create instance without ID (database will auto generate one if you specified an
id column)*/
ToolsByChannelIdDbModel({
this.lifeCycle,
this.ownerPartyRoleId,
this.triggerImages,
this.triggerImagesUrl,
this.triggerChannelIds,
this.actionImages,
this.actionImagesUrl,
});
// Create instance with ID
ToolsByChannelIdDbModel.withId({
this.productCoreId,
this.lifeCycle,
this.ownerPartyRoleId,
this.triggerImages,
this.triggerImagesUrl,
this.triggerChannelIds,
this.actionImages,
this.actionImagesUrl,
});
/* When you want to add object to database you can use .toMap() this will
convert ToolsByChannelIdDbModel instance to a Map*/
Map<String, dynamic> toMap()
{
var tempMap = Map<String, dynamic>();
if(productCoreId != null)
{
tempMap['productCoreId'] = productCoreId;
}
tempMap['lifeCycle'] = lifeCycle;
tempMap['ownerPartyRoleId'] = ownerPartyRoleId;
tempMap['triggerImages'] = triggerImages;
tempMap['triggerImagesUrl'] = List<dynamic>.from(triggerImagesUrl.map((x) =>
x));
tempMap['triggerChannelIds'] = List<dynamic>.from(triggerChannelIds.map((x) =>
x));
tempMap['actionImages'] = actionImages;
tempMap['actionImagesUrl'] = List<dynamic>.from(actionImagesUrl.map((x) =>
x));
return tempMap;
}
/* Named constructor to create ToolsByChannelIdDbModel instance from a Map that you
get from database */
ToolsByChannelIdDbModel.fromMap(Map<String, dynamic> map)
{
this.productCoreId = map['productCoreId'];
this.lifeCycle = map['lifeCycle'];
this.ownerPartyRoleId = map['ownerPartyRoleId'];
this.triggerImages = map['triggerImages'];
this.triggerImagesUrl = List<String>.from(map['triggerImagesUrl'].map((x) =>
x));
this.triggerChannelIds = List<int>.from(map['triggerChannelIds'].map((x) =>
x));
this.actionImages = map['actionImages'];
this.actionImagesUrl = List<dynamic>.from(map['actionImagesUrl'].map((x) =>
x));
}
}
希望这有所帮助!