NoSuchMethodError(NoSuchMethodError:方法'[]'在null上被调用。接收者:null尝试调用:[](“title”))

Posted

技术标签:

【中文标题】NoSuchMethodError(NoSuchMethodError:方法\'[]\'在null上被调用。接收者:null尝试调用:[](“title”))【英文标题】: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,",
    ,
  ]

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

【问题讨论】:

【参考方案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']); 起作用了。

以上是关于NoSuchMethodError(NoSuchMethodError:方法'[]'在null上被调用。接收者:null尝试调用:[](“title”))的主要内容,如果未能解决你的问题,请参考以下文章

NoSuchMethodError:null 上的无效成员:'length'

NoSuchMethodError,小部件库捕获的异常

NoSuchMethodError - 颤振[重复]

接缝测试 NoSuchMethodError

NoSuchMethodError:null 上的无效成员:'_get'

Flink:java.lang.NoSuchMethodError:AvroSchemaConverter