Is there a way to parse a JSON as a Map<String, String> instead of Map<String, dynamic> when using Dart's json.decode.
For example with JSON of:
{
'a': 2,
'b': 'c'
}
It would parse into:
{
'a': '2',
'b': 'c'
}
Sadly, no. The code for decoding a Map starts with a Map<String, dynamic> and adds values as they are read, so there is no way to make the value type more specific.
Look into these for other options:
Map with the right type from any source map: https://api.dartlang.org/stable/2.1.0/dart-core/Map/Map.from.html – you pay a one-time cost for copying the values.Map - https://api.dartlang.org/stable/2.1.0/dart-core/Map/cast.html – no copy cost, but you pay the overhead of wrapping/casting on each access of the original Map2 as a string would be a violation of the JSON specification, that's not something the JSON parse will do for you. It sounds like you need to convert the map. You can do: var jsonMap = jsonDecode(...); Map<String, String> myMap = Map.fromEntries(jsonMap.entries.map((e) => MapEntry(e.key, "${e.value}")));.