【问题标题】:NoSuchMethodError (NoSuchMethodError: The method '[]' was called on null. Receiver: null Tried calling: []("title"))NoSuchMethodError(NoSuchMethodError:方法'[]'在null上被调用。接收者:null尝试调用:[](“title”))
【发布时间】: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,",
    },
  ]
}

你能告诉我我做错了什么吗?

【问题讨论】:

    标签: json flutter


    【解决方案1】:

    您的响应格式与您编码的格式不同。

    结构是这样的:

    {
    "context": {
        "categories": [
            {
                "id": 15,
                "title": "Auto-Moto",
                "slug": "auto-moto",
                "content": "Vanzari-cumparari de automobile, piese acte. Anunţurile sunt destinate zonei Radauti-Suceava<br />",
                "related": {
                    "link": "/anunturi/auto-moto-15",
                    "archiveLink": "/arhiva-anunturi/c/auto-moto-15",
                    "deletedPostCount": 387,
                    "postCount": 22
                }
            },
    

    因此,要获取作业列表,您应该相应地对其进行解析。进入工作列表的正确语句应该是这样的:

    print(jsonResponse['context']['categories'])
    

    为了能够创建作业模型列表,请尝试以下操作:

    var json = jsonResponse['context']['categories'];
    var jobList = List<JobModel>();
    json.forEach((key, value) {
      print(key);
      JobModel job = JobModel.fromJson(value);
      jobList.add(job);
    });
    print(jobList);
    

    试试这个 sn-p:

    Future<List<JobModel>> _getJobs() async {
         var response = await http.get(
        "https://www.eradauti.ro/api/context?pathname=/anunturi/pagina-1&userID=");
         jsonResponse = json.decode(response.body);
         var json = jsonResponse['context']['categories'];
         var jobList = List<JobModel>();
         json.forEach((key, value) {
           print(key);
           JobModel job = JobModel.fromJson(value);
           jobList.add(job);
         });
         print(jobList);
    }
    

    【讨论】:

    • 如果我把var json = jsonResponse['context']['categories'] 放在jsonResponse = json.decode(response.body) 之前,我似乎不知道我应该把它放在哪里,没有任何反应,没有错误,如果我把它放在我收到错误Local variable 'json' can't be referenced before it is declared. 之后。但是print(jsonResponse['context']['categories']); 起作用了。
    猜你喜欢
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 2021-10-25
    • 2020-10-12
    • 1970-01-01
    相关资源
    最近更新 更多