I'm attempting to convert JSON that has strings for keys and string arrays for values.
From my understanding, this should work:
import 'dart:convert';
void main() {
var jsonString = '{"key": ["1", "2", "3"]}';
var data = json.decode(jsonString) as Map;
var result = data.cast<String, List<String>>();
print(result);
}
However I get the error that type 'List<dynamic>' is not a subtype of type 'List<String>' in type cast.
What's interesting, however, is that the following does correctly work:
import 'dart:convert';
void main() {
var jsonString = '{"key": "value"}';
var data = json.decode(jsonString) as Map;
var result = data.cast<String, String>();
print(result);
}
So, I assume that the .cast<> method introduced with Dart 2 doesn't know how to convert nested types that aren't simple types like String, int, or bool.
How would I convert this object to a Map<String, List<String>> without resorting to external libraries?