I am trying to parse data from a Rest API inside a Dart/Flutter application. The JSON contains a field called data at the root, which contains a list of Words. I want to get a List<Word> from this JSON. I already have the following code:
Map<String, dynamic> jsonMap = json.decode(jsonString);
List<Word> temp = jsonMap['data']
.map((map) => map as Map<String, dynamic>)
.map((Map<String, dynamic> map) => Word.fromJson(map)).toList(); // map to List<Word>
Word.fromJson has the following signature:
Word.fromJson(Map<String, dynamic> json)
The final call to map gives the following error:
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>'
From my understanding, the call to map((map) => map as Map<String, dynamic>) should convert the List<dynamic> to a List<Map<String, dynamic>>, so I am confused as to why I get the error.
Any advice appreciated.