【发布时间】:2021-01-20 10:17:53
【问题描述】:
我正在寻找一种将Map<int, List<int>> 对象转换为字符串(反之亦然)的方法,以便能够使用 sqflite 将其保存到我的数据库中。
class ClassToBeSavedInDB {
final String name;
final Map<int, List<int>> listByIDs;
ClassToBeSavedInDB({this.name, this.listByIDs});
Map<String, dynamic> toMap() {
return {
'name': name,
'listByIDs': listByIDs, // TODO: convert this to a String
};
}
}
我尝试按照类似问题here 中的建议使用json.encode(map),但出现以下异常:
: Converting object to an encodable object failed: Instance of 'JsLinkedHashMap<int, List<int>>'Error: Converting object to an encodable object failed: Instance of 'JsLinkedHashMap<int, List<int>>'
更新
看起来 jsonDecode 不喜欢 int 键。使用Map<String, List<int>> 解决了这个问题。
【问题讨论】: