I know how to convert Map<dynamic, dynamic> to Map<String, dynamic> using Map.from() method.
But what if I have unspecified number of nested Maps inside? How to convert all potential children as well from Map<dynamic, dynamic> to Map<String, dynamic>?
-
1It will be easy to understand if you will add response map dataJahidul Islam– Jahidul Islam2021-12-21 12:06:43 +00:00Commented Dec 21, 2021 at 12:06
Add a comment
|
4 Answers
Same answer as fravolt's but I was unable do put code in comments:
Map.forEach do not treats value by reference. You may change to:
// recursively convert the map
Map<String, dynamic> convertMap(Map<dynamic, dynamic> map) {
for (var key in map.keys) {
if (map[key] is Map) {
map[key] = convertMap(map[key]);
}
} // use .from to ensure the keys are Strings
return Map<String, dynamic>.from(map);
// more explicit alternative way:
// return Map.fromEntries(map.entries.map((entry) => MapEntry(entry.key.toString(), entry.value)));
}
Comments
Same answer but with additional condition for List
/// recursively convert the map tp Map<String,dynamic>
Map<String, dynamic> convertMap(Map<dynamic, dynamic> map) {
for (var key in map.keys) {
if (map[key] is Map) {
map[key] = convertMap(map[key]);
} else if (map[key] is List) {
map[key] = map[key].map((e) {
if (e is Map) {
return convertMap(e);
}
return e;
}).toList();
}
}
return Map<String, dynamic>.from(map);
}
Comments
You could use a recursive approach to this problem, where all map values of type Map are recursively converted as well.
// recursively convert the map
Map<String, dynamic> convertMap(Map<dynamic, dynamic> map) {
map.forEach((key, value) {
if (value is Map) {
// it's a map, process it
value = convertMap(value);
}
});
// use .from to ensure the keys are Strings
return Map<String, dynamic>.from(map);
// more explicit alternative way:
// return Map.fromEntries(map.entries.map((entry) => MapEntry(entry.key.toString(), entry.value)));
}
// example nested map with dynamic values
Map<dynamic, dynamic> nestedMap = {
'first': 'value',
'second': {
'foo': 'bar',
'yes': 'ok',
'map': {'some': 'value'},
}
};
// convert the example map
Map<String, dynamic> result = convertMap(nestedMap);
print(result);
4 Comments
Adam Smaka
this is a great idea but i still get this error '"type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast"'. what am I doing wrong?
fravolt
Maybe it works for you if you explicitly parse the keys to Strings? I added the alternative return line where the entry keys are first mapped to Strings to the code above
Adam Smaka
ok it works fine now, there was a problem with nested parsing with freezed. thank you for your help :)
Map<String, dynamic> convertMap(Map<dynamic, dynamic> map) {
map.forEach((key, value) {
if (value is Map) {
// logger.d("key: $key ${value.runtimeType}");
// it's a map, process it
map[key] = convertMap(value);
// logger.d("key: $key ${value.runtimeType}");
}
});
// use .from to ensure the keys are Strings
return Map<String, dynamic>.from(map);
// more explicit alternative way:
// return Map.fromEntries(map.entries.map((entry) => MapEntry(entry.key.toString(), entry.value)));
}