【发布时间】:2018-08-15 19:14:32
【问题描述】:
我想将以下 json 结构映射到模型。我尝试将数组映射到一个列表,然后将每个集合解析为模型。
将显示以下错误:
类型“MappedListIterable”不是类型“List”的子类型
json
{
"objectId": "vbbZIPV6qs",
"sets": [
{
"repetitions": 10,
"weight": 10,
"time": 0
}
],
"description": "",
"type": "EXERCISE",
}
颤动
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,
);
}
}
【问题讨论】: