Bill_The_Coder
Bill_The_Coder

Reputation: 2510

Getting difficulty from Reading JSON data Flutter

This is my JSON data Structure:

[
  [
   {
    "nos": 0,
     "name": "A S MUSIC AND DANCE A CULTURAL ORGANIZATION",
    "unique_id": "AN/2020/0259067",
    "reg_details": [
     {
      "registered_with": "Registrar of Societies"
      },
     {
      "type_of_ngo": "Registered Societies (Non-Government)"
     },
     {
      "registration_no": "1534"
       },
       {
         "copy_of_registration_certificate": "Available"
          },
       {
         "copy_of_pan_card": "Available"
        },
       {
        "act_name": "Registration Act,1860"
        },
      {
       "city_of_registration": "Port Blair"
      },
       {
       "state_of_registration": "ANDAMAN & NICOBAR ISLANDS"
        },
         {
       "date_of_registration": "25-05-2016"
        }
      ],  

In my this Function:

 Future<void> _NgoDetail() async {
    String jsonString = await _loadANgoAsset();
    final jsonResponse = json.decode(jsonString);
    var rest = jsonResponse[0] as List;
    List list=[];
    list= rest.map<NGO>((json) => NGO.fromJson(json)).toList();
    debugPrint(list[0].regDetails[1].type_of_ngo);
  }

When I run this line:

debugPrint(list[0].regDetails[1].type_of_ngo);

Results are fine i.e

I/flutter ( 5579): List<dynamic>
I/chatty  ( 5579): uid=10326(com.skillzup.mn.mn) 1.ui identical 117 lines
I/flutter ( 5579): List<dynamic>
I/flutter ( 5579): Registered Societies (Non-Government)

But when I run this:

debugPrint(list[0].regDetails[2].registration_no);

I get this error:

I/flutter ( 5579): List<dynamic>
I/chatty  ( 5579): uid=10326(com.skillzup.mn.mn) 1.ui identical 117 lines
I/flutter ( 5579): List<dynamic>
I/flutter ( 5579): null

And same for this:

debugPrint(list[0].regDetails[3].copy_of_registration_certificate);

Error:

Unhandled Exception: NoSuchMethodError: Class 'RegDetails' has no instance getter 'copy_of_registration_certificate'.

Most Important data that I want from this API is: debugPrint(list[2].regDetails[8].date_of_registration);

But I get this error:

I/flutter ( 5579): List<dynamic>
I/chatty  ( 5579): uid=10326(com.skillzup.mn.mn) 1.ui identical 117 lines
I/flutter ( 5579): List<dynamic>
I/flutter ( 5579): null

For some fields, data is showing correct, but for some, either null or either some error. What am I missing?

This is My NGO and Registeration Class:

class NGO {
  String name;
  String id;
  List<RegDetails> regDetails;
  contactDetails ContactDetails;
  Members members;
  WorkingSectors workingSectors;

  NGO(
      {this.members, this.regDetails, this.name, this.workingSectors, this.ContactDetails, this.id,});

  
  factory NGO.fromJson(Map<String, dynamic> json) {
    var list = json['reg_details'] as List;
    print(list.runtimeType);
    List<RegDetails> regDetails = list.map((i) => RegDetails.fromJson(i)).toList();


    return NGO(
      id: json["unique_id"],
      name: json['name'],
     regDetails: regDetails


    );
  }
}

class RegDetails {
  String registration_no;
  String type_of_ngo;
  String registered_with;
  String copy_of_pan_card;
  String date_of_registration;
  String city_of_registration;
  String state_of_registration;
  String FCRA;

  RegDetails(
      {this.FCRA = 'Not Available', this.city_of_registration, this.copy_of_pan_card, this.date_of_registration, this.registered_with, this.registration_no, this.state_of_registration, this.type_of_ngo});

  factory RegDetails.fromJson(Map<String, dynamic> json){
    return RegDetails(
      registration_no: json['registeration_no'],
      type_of_ngo: json['type_of_ngo'],
      registered_with: json['registered_with'],
      copy_of_pan_card: json['copy_of_pan_card'],
      date_of_registration: json['date_of_registeration'],
      city_of_registration: json['city_of_registeration'],
      state_of_registration: json['state_of_registeration'],
    );
  }
}

Upvotes: 0

Views: 37

Answers (1)

Arslan Kaleem
Arslan Kaleem

Reputation: 1618

Due to spelling mistake in this block of code

  factory RegDetails.fromJson(Map<String, dynamic> json){
    return RegDetails(
      registration_no: json['registration_no'],
      type_of_ngo: json['type_of_ngo'],
      registered_with: json['registered_with'],
      copy_of_pan_card: json['copy_of_pan_card'],
      date_of_registration: json['date_of_registeration'],
      city_of_registration: json['city_of_registeration'],
      state_of_registration: json['state_of_registeration'],
    );
  }
}

Upvotes: 1

Related Questions