Reputation: 5867
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'
}
Upvotes: 0
Views: 108
Reputation: 6171
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 MapUpvotes: 1