【问题标题】:Error while Adding JSON data to the list in flutter在颤动中将JSON数据添加到列表时出错
【发布时间】:2021-01-25 22:08:38
【问题描述】:

以下是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 时,出现以下错误

未处理的异常:NoSuchMethodError:调用了方法“add” 空值。接收方:null 尝试调用:add(Instance(length:7) of '_GrowableList') #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)

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

【问题讨论】:

    标签: json list flutter dart flutter-listview


    【解决方案1】:

    你需要初始化notificationsList:

    List notificationsList = [];
    

    由于list 是可迭代的,因此使用addAll()

    【讨论】:

      猜你喜欢
      • 2020-12-24
      • 2023-03-03
      • 2021-09-20
      • 2020-10-15
      • 2020-03-23
      • 2021-08-29
      • 2021-06-05
      • 2021-03-18
      • 2021-03-08
      相关资源
      最近更新 更多