在 flutter 中将 JSON 数据添加到列表时出错

Error while Adding JSON data to the list in flutter

下面是JSON在flutter应用中从服务器成功获取到的数据

{
    "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 on null. 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()