flutter中的空指针异常

问题描述 投票:0回答:1

我正在尝试检索教区列表并将它们分配给模型类,以便我可以一一引用它们,但我收到了对空值进行空检查的错误。这是堆栈跟踪。 E/flutter (17874): [错误:flutter/runtime/dart_vm_initializer.cc(41)] 未处理的异常:空值检查运算符 [ ] E/flutter (17874): #0 新的 ParishResponse.fromJson。 (包:parish_aid_admin/features/home/data/model/parish_model.dart:42:13)

    class ParishResponse {
      int? code;
      String? title;
      String? message;
      List<ParishData>? data;
    
      ParishResponse(this.code, this.title, this.message, this.data);
    
      ParishResponse.fromJson(Map<String, dynamic> json) {
        code = json["code"];
        title = json["title"];
        message = json["message"];
        if (json['data'] != null) {
          json['data'].forEach((e) {
            //This printing statement returns all the parishes
            //With their correct data

            print("This are the parishes ${e.toString()}");
    
            //This other printing statement returns all the name
            //of parishes that are in the list
            print("The parish name in the list are ${ParishData.fromJson(e).name}");
    
            //But here is giving me an error that says
            //Null check operator used on a null value
            //when I tried to retrieved
            //each parish info and assign it to the object of ParishData.
            //Please why is it null when the data retrieved from the server is not null?
            //or is my implementation wrong?
            data!.add(ParishData.fromJson(e));
          });
        }
      }
    }
    
    class ParishData {
      int? id;
      String? name;
      String? acronym;
      String? address;
      String? phoneNo;
      String? parishPriestName;
      Town? town;
      State? state;
      Country? country;
      Diocese? diocese;
      Attachments? attachments;
    
      ParishData(
          {this.id,
          this.name,
          this.acronym,
          this.address,
          this.phoneNo,
          this.parishPriestName,
          this.town,
          this.state,
          this.country,
          this.diocese,
          this.attachments});
    
      ParishData.fromJson(Map<String, dynamic> json) {
        id = json['id'];
        name = json['name'];
        acronym = json['acronym'];
        address = json['address'];
        phoneNo = json['phone_no'];
        parishPriestName = json['parish_priest_name'];
        if (json['town'] != null) {
          town = Town.fromJson(json['town']);
        }
        if (json['state'] != null) {
          state = State.fromJson(json['state']);
        }
        if (json['country'] != null) {
          country = Country.fromJson(json['country']);
        }
        if (json['diocese'] != null) {
          diocese = Diocese.fromJson(json['diocese']);
        }
        if (json['attachments'] != null) {
          attachments = Attachments.fromJson(json['attachments']);
        }
      }
    }
flutter nullpointerexception
1个回答
0
投票

data
null
,而不是
json['data']
。只需在尝试向其中添加内容之前对其进行初始化即可。太喜欢了

        if (json['data'] != null) {
          data = []; //add this
          json['data'].forEach((e) {
            data!.add(ParishData.fromJson(e));
          });
        }
© www.soinside.com 2019 - 2024. All rights reserved.