每当我运行Flutter
应用时,我都会收到此错误:
E / flutter(8247):NoSuchMethodError:方法'添加'在null上调用。
class _HomePageState extends State<HomePage> {
List responseJson;
List EventDetails ;
Future<String> fetchPost() async {
final response = await http.get(
"https://....",
headers: {
HttpHeaders.AUTHORIZATION:
"Bearer .."
});
for(var i = 0; i< (responseJson?.length ?? 0);i++) {
print(responseJson[i]["id"]);
final eventDetailsRepsonse = await http.get(
"https:..." + responseJson[i]["id"].toString(),
headers: {
HttpHeaders.AUTHORIZATION:
"Bearer .."
});
EventDetails.add(json.decode(eventDetailsRepsonse.body));
EventDetails = responseJson;
}
//print(response.body);
this.setState(() {
responseJson = json.decode(response.body);
print(response.body);
print(responseJson);
});
// data = JSON.decode(response.body);
print(responseJson[1]["code"]);
print(responseJson[1]["event_banner"]);
//return new Post.fromJson(responseJson);
}
任何想法如何解决这个问题?
答案 0 :(得分:0)
responseJson
尚未初始化
for(var i = 0; i<responseJson.length;i++) {
已执行。
将其更改为
for(var i = 0; i< (responseJson?.length ?? 0);i++) {
可能会修复它。