【发布时间】:2020-09-15 19:07:41
【问题描述】:
从 json 获取数据后出现此错误;
我的模型是这样的:
class JobModel {
final String title;
final int id;
final String slug;
final String content;
JobModel(this.title, this.id, this.slug, this.content);
JobModel.fromJson(Map<String, dynamic> json)
: title = json['title'],
id = json['id'],
slug = json['slug'],
content = json['content'];
}
这是我从 json 文件中获取数据的函数:
List<JobModel> jobList;
Map<String, dynamic> jsonResponse;
Future<List<JobModel>> _getJobs() async {
var response = await http.get(
"https://www.eradauti.ro/api/context?pathname=/anunturi/pagina-1&userID=");
this.setState(() {
jsonResponse = json.decode(response.body);
});
jobList = List<JobModel>();
jsonResponse.forEach((key, value) {
JobModel job = JobModel.fromJson(value);
jobList.add(job);
});
print(jsonResponse[0]['title']); //shows null
return jobList;
}
我的 json 文件如下所示:
{
"context": [
// first JobModel Map
{
"title": "some title here",
"id": 1234,
"slug": "some slug value",
"content": "the rest of the object content",
},
// second JobModel Map
{
"title": "some other title here",
"id": 5678,
"slug": "some other slug value",
"content": "blah blah,",
},
]
}
你能告诉我我做错了什么吗?
【问题讨论】: