I would like to map the following json structure to models. I try to map the array to an List and than parse each set to model.
The following error would be displayed:
type 'MappedListIterable' is not a subtype of type 'List'
Json
{
"objectId": "vbbZIPV6qs",
"sets": [
{
"repetitions": 10,
"weight": 10,
"time": 0
}
],
"description": "",
"type": "EXERCISE",
}
Flutter
class PlanItem {
String type;
String description;
List<Set> sets = [];
PlanItem(this.type, this.description, this.sets);
factory PlanItem.fromJson(Map<String, dynamic> json) {
return PlanItem(
json['type'],
json['description'],
(json['sets'] as List).map((i) {
return Set.fromJson(i);
}).toList(),
);
}
}
class Set {
int repetitions;
int weight;
int time;
Set(this.repetitions, this.weight, this.time);
// convert Json to an exercise object
factory Set.fromJson(Map<String, dynamic> json) {
return Set(
json['repetitions'] as int,
json['weight'] as int,
json['time'] as int,
);
}
}
