我试图从 JSON 获取信息,但我遇到了问题。
这是 JSON 文件:
{
"version": "v1",
"resource": "***",
"action": "show",
"method": "GET",
"query": {
"type": "list",
"query": null,
"pagination": null
},
"request": [],
"data": {
"Total": [
{
"id": 34724,
"rank": 1,
"loss": 0,
}
....
],
"Away": [
{
"id": 3125"
"rank": 0"
}
....
]
}
}
这就是我试图解码它并存储数据的方式:
class Standings {
final String version;
final String resource;
final String action;
final String method;
List<Data> datas;
Standings(
{this.version, this.resource, this.action, this.method, this.datas});
factory Standings.fromJson(Map<String, dynamic> parsedJson) {
var listD = parsedJson['data'];
print(listD.runtimeType);
List<Data> dataList = listD.map((i) => Data.fromJson(i)).toList();
return Standings(
version: parsedJson['version'],
resource: parsedJson['resource'],
action: parsedJson['action'],
method: parsedJson['method'],
datas: dataList,
);
}
}
class Data {
List<Total> total;
List<Away> away;
Data(
{this.total, this.away});
factory Data.fromJson(Map<String, dynamic> parsedJson) {
var listT = parsedJson['Total'] as List;
print(listT.runtimeType);
List<Total> totalList = listT.map((i) => Total.fromJson(i)).toList();
var listA = parsedJson['Away'] as List;
print(listA.runtimeType);
List<Away> awayList = listA.map((i) => Away.fromJson(i)).toList();
return Data(
total: totalList,
away: awayList,
);
}
}
class Total {
final int id;
final int rank;
final int loss;
Total({
this.id,
this.rank,
this.loss,
});
factory Total.fromJson(Map<String, dynamic> parsedJson) {
return Total(
id: parsedJson['id'],
rank: parsedJson['rank'],
loss: parsedJson['loss'],
);
}
}
和同样的事情,但我得到这个错误:
数据在 null 上调用
I/flutter(14264):══╡WIDGETS LIBRARY 的例外情况
╞═════════════════════════════════════════════════ ══════════
I/flutter(14264):抛出以下 NoSuchMethodError 构建
Schedule(dirty,州:
I/flutter(14264):_ScheduleState#2dfb0(ticker inactive)):
I/flutter(14264):getter'datas'在 null 上调用。
I/flutter(14264):接收者:null
I/flutter(14264):尝试呼叫:数据
我该怎么做才能解决这个问题?