我正在尝试将Map<String,dynamic>转换为Map<String, Map<String, String>>
Map<String,dynamic> oldMap = querySnapshot.docs.first.data()["cach"];
Map<String, Map<String, String>> newMap = oldMap.map((a, b) => MapEntry(a, b as Map<String, String>)); 但我有个错误:
Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, String>' in type cast发布于 2021-02-17 23:55:43
import 'dart:convert';
const rawData = '''
{"a": {"b": "c"}, "d":{"e": "f"}}
''';
Map<String, String> convert(Map<String, dynamic> data) {
return Map<String,String>.fromEntries(data.entries.map<MapEntry<String,String>>((me) => MapEntry(me.key, me.value)));
}
void main(List<String> args) {
var oldMap = jsonDecode(rawData) as Map<String, dynamic>;
var newMap = Map.fromEntries(oldMap.entries.map((me) => MapEntry(me.key, convert(me.value))));
print(newMap.runtimeType);
}结果:
_InternalLinkedHashMap<String, Map<String, String>>https://stackoverflow.com/questions/66249980
复制相似问题