gpt4 book ai didi

json - 将JSON数据添加到列表中时出错

转载 作者:行者123 更新时间:2023-12-03 04:35:05 24 4
gpt4 key购买 nike

以下是在flutter应用程序中从服务器成功检索到的JSON数据

{
"error": "false",
"notification": [
{
"rn": "1",
"id": "224",
"company_details": {
"code": "2",
}
},
{
"rn": "2",
"id": "219",
"company_details": {
"code": "3",
}
},
{
"rn": "3",
"id": "213",
"company_details": {
"code": "3",
}
},
{
"rn": "4",
"id": "209",
"company_details": {
"code": "4",
}
},
{
"rn": "5",
"id": "204",
"company_details": {
"code": "3",
}
},
{
"rn": "6",
"id": "199",
"company_details": {
"code": "3",
}
},
{
"rn": "7",
"id": "193",
"company_details": {
"code": "3",
}
}
],
}
此处使用的代码如下
List notificationsList;
getnotifications(int page) async {

Map data = {
'user_id': “VICKY,
"page":page.toString()
};


var response = await http.post(companyorders, body: data);
if(response.statusCode == 200) {

jsonResponse = json.decode(response.body);
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');


String errorcheck = jsonResponse['error'];




companyDetail = NotificationModel.fromJson(json.decode(response.body));

companyDetail = NotificationModel.fromJson(json.decode(response.body));

print('names of companies');
print(companyDetail.content);

List list = jsonResponse['notification'];

notificationsList.add(list);

}

}
当我尝试将 list添加到 notificationsList时,出现以下错误

Unhandled Exception: NoSuchMethodError: The method 'add' was called onnull. Receiver: null Tried calling: add(Instance(length:7) of'_GrowableList')#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)


我想将检索到的数据添加到 notificationsList中,该怎么做

最佳答案

您需要初始化notificationsList:

List notificationsList = [];
另外,由于 list是可迭代的,因此请使用 addAll()

关于json - 将JSON数据添加到列表中时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64303551/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com