如何编码此地图 地图<int, 列表<int>>?



嗨,我在编码这种映射时遇到问题:map<int,列表<int>gtblocksMap有什么建议吗?causejson.encode(blocksMap(给出错误将对象转换为可编码对象失败:_LinkedHashMap

这是我试图编码的数据示例:

地图<int,列表<int>gt;块映射:

{
0: [18, 117, 26, 200, 4, 30, 43, 110],
1: [18, 117, 26, 200, 4, 30, 43, 110], 
2: [18, 117, 26, 200, 4, 30, 43, 110], 
3: [18, 117, 26, 200, 4, 30, 43, 110], 
4: [18, 117, 26, 200, 4, 30, 43, 110], 
5: [18, 117, 26, 200, 4, 30, 43, 110], 
6: [18, 117, 26, 200, 4, 30, 43, 110], 
7: [18, 117, 26, 200, 4, 30, 43, 110], 
8: [18, 117, 26, 200, 4, 30, 43, 110], 
9: [18, 117, 26, 200, 4, 30, 43, 110], 
10: [18, 117, 26, 200, 4, 30, 43, 110], 
11: [18, 117, 26, 200, 4, 30, 43, 110]
}

建议如何编码这个地图将是伟大的,谢谢!

您可以将映射转换为字符串映射和字符串列表

final Map<String, List<int>> blocksMapOfIntToString = {
for (var item in blocksMap.entries) '${item.key}': item.value,
};

然后对进行编码

final encoded = jsonEncode(blocksMapOfIntToString);

只要你需要你的数据,你就可以解码

final decoded = jsonDecode(encoded);

然后转换回

final Map<int, List<int>> blocksMapOfStringToInt = {
for (var item in decoded.entries)
int.parse(item.key): List<int>.from(item.value),
};

最新更新