我有一个值列表,该值列表在模型内部。如何一次性将这些数据保存在数据库中? 我创建了一个模型类并转换为JSON,并尝试保存数据但显示异常 未处理的异常:
DatabaseException(java.util.HashMap无法强制转换为 java.lang.Integer)
Future<VendorEventCommitListModel> saveVendorEventCommitList(VendorEventCommitListModel vendorEventCommitList) async {
var dbClient = await db;
// this will insert the Album object to the DB after converting it to a json
// int res = await dbClient.rawInsert('INSERT INTO vendor_event_commit_list() VALUES(?,?,?,?,?,?,?,?,?,?)', [userDetailsDBModel.getName,userDetailsDBModel.getEmail,userDetailsDBModel.getUsertype,userDetailsDBModel.getPhone1,userDetailsDBModel.getPhone2,userDetailsDBModel.getGrnder,userDetailsDBModel.getAddress,userDetailsDBModel.getCity,userDetailsDBModel.getState,userDetailsDBModel.getCountry]);
vendorEventCommitList.eventId = await dbClient.insert('vendor_event_commit_list', vendorEventCommitList.toJson());
return vendorEventCommitList;
}
映射器模型看起来像
Map<String, dynamic> toJson() => {
"eventId": eventId,
"venderEmail": venderEmail,
"username": username,
"date": date,
"eventDateTime": eventDateTime,
"eventName": eventName,
"utensils": utensils,
"hotServe": hotServe,
"carting": carting,
"eventAddress": eventAddress,
"remarks": remarks,
"Vgccourse": List<dynamic>.from(Vgstr.map((x) => x.toJson())),
"Vgstr": List<dynamic>.from(Vgstr.map((x) => x.toJson())),
}
下面是api响应。
[{
"event_id": 2,
"Vender_Email": null,
"username": "neenabastin@gmail.com",
"date": "2019-12-06T11:38:19.7681942+05:30",
"Event_date_time": "2019-11-06T12:18:35.943",
"Event_name": "Marriage",
"Utensils": 0,
"Hot_serve": 0,
"Carting": 0,
"Event_Address": "",
"Remarks": "",
"itemdetails": null,
"Vgccourse": [
{
"Vgccourseitem": "Appam",
"Vgccoursecount": 67
},
{
"Vgccourseitem": "Appam",
"Vgccoursecount": 67
}
],
"Vgstr": [
{
"Vgstritem": "item",
"Vgstr_count": 56
},
{
"Vgstritem": "item",
"Vgstr_count": 56
},
]
},
{
"event_id": 2,
"Vender_Email": null,
"username": "neenabastin@gmail.com",
"date": "2019-12-06T11:38:19.7681942+05:30",
"Event_date_time": "2019-11-06T12:18:35.943",
"Event_name": "Marriage",
"Utensils": 0,
"Hot_serve": 0,
"Carting": 0,
"Event_Address": "",
"Remarks": "",
"itemdetails": null,
"Vgccourse": [
{
"Vgccourseitem": "Appam",
"Vgccoursecount": 67
},
{
"Vgccourseitem": "Appam",
"Vgccoursecount": 67
}
],
"Vgstr": [
{
"Vgstritem": "item",
"Vgstr_count": 56
},
{
"Vgstritem": "item",
"Vgstr_count": 56
},
]
}
]
答案 0 :(得分:0)
您可以轻松地将列表另存为JSON对象字符串,或者必须创建主明细表