flutternullpointerexception

null pointer exception in flutter


I am trying to retrieve a list of Parishes and assign them to a model class so that I can reference them one by one but I got this error of null check on a null value. This is the stack trace.

E/flutter (17874): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value [ ] E/flutter (17874): #0 new ParishResponse.fromJson. (package: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']);
        }
      }
    }

Solution

  • It's data that is null, not json['data']. Simply initialize it before trying to add stuff to it. So like

            if (json['data'] != null) {
              data = []; //add this
              json['data'].forEach((e) {
                data!.add(ParishData.fromJson(e));
              });
            }