bensofter
bensofter

Reputation: 812

Unexpected error while parsing JsonData in flutter

Am trying to parse json list data so I could save into an object. But I kept getting errors each time and I do not know why.

Json Data

{
    "status": "success",
    "data": [
        {
            "distro_name": "Ikeja Electric (IKEDC)",
            "service_id": "ikeja-electric",
            "type": [
                "prepaid",
                "postpaid"
            ]
        },
        {
            "distro_name": "Eko Electric (EKEDC)",
            "service_id": "eko-electric",
            "type": [
                "prepaid",
                "postpaid"
            ]
        },
        {
            "distro_name": "Ibadan Electric (IBEDC)",
            "service_id": "ibadan-electric",
            "type": [
                "prepaid",
                "postpaid"
            ]
        },
        {
            "distro_name": "Kano Electric (KEDCO)",
            "service_id": "kano-electric",
            "type": [
                "prepaid",
                "postpaid"
            ]
        },
        {
            "distro_name": "Jos Electricity Distribution (JED)",
            "service_id": "jos-electric",
            "type": [
                "prepaid",
                "postpaid"
            ]
        },
        {
            "distro_name": "Port-Harcourt Electric (PHED)",
            "service_id": "portharcourt-electric",
            "type": [
                "prepaid",
                "postpaid"
            ]
        }
    ]
}

This is my model class for the data.

Model

class Providers {
  String distro_name;
  String service_id;
  dynamic type;

  Providers(this.distro_name, this.service_id, this.type);


  Providers.fromJson(Map<String, dynamic> json):
        distro_name = json['distro_name'],
        service_id = json['service_id'],
        type = json['type'];

}

Main.dart

fetchProviders() async{
    try {
      final response = await http.get(
        uri,
        headers: {'Content-Type': 'application/json','Authorization': 'Bearer ' + _bloc.bearerToken, },
      );
      final responseJson = json.decode(response.body);
      for (var u in responseJson["data"]) {
       Providers provider = Providers(
            u["distro_name"],
            u["service_id"],
             u["list"]);
      providerList.add(provider);
      }
      return responseJson;
    } catch (exception) {
      print(exception);
    }
  }

This is the error I get.

 NoSuchMethodError: The method 'add' was called on null.
I/flutter ( 1366): Receiver: null

I don't really know why I get this error. I don't have null values on my api and I have consumed these kind of json structure in the past.

Upvotes: 0

Views: 825

Answers (3)

Safwan Mamji
Safwan Mamji

Reputation: 126

model

       class Providers {
        String distro_name;
        String service_id;
       dynamic type;

 Providers(this.distro_name, this.service_id, this.type);


 factory Providers.fromJson(Map<String, dynamic> json){

    return Providers(
       distro_name = json['distro_name'],
       service_id = json['service_id'],
        type = json['type'],
     );
     }
     }

main.dart

fetchProviders() async{
try {
  final response = await http.get(
    uri,
    headers: {'Content-Type': 'application/json','Authorization': 'Bearer ' + _bloc.bearerToken, },
  );
  final responseJson = json.decode(response.body);
  for (var u in responseJson['data']) {
   Providers provider = Providers.fromJson(u);
   providerList.add(provider);
 return responseJson;
     }
  } 
  catch (exception) {
  print(exception);
    }
  }

Upvotes: 0

Kahou
Kahou

Reputation: 3488

Initialize providerList:

var providerList = </* type */>[]
      for (var u in responseJson["data"]) {
       Providers provider = Providers(
            u["distro_name"],
            u["service_id"],
             u["list"]);
      providerList.add(provider);
      }

Using list map:

providerList = responseJson["data"].map((u) => Providers(
                 u["distro_name"],
                 u["service_id"],
                 u["list"],
               )).cast<String>().toList();

Upvotes: 1

Raine Dale Holgado
Raine Dale Holgado

Reputation: 3460

Model

class Providers {
  String distro_name;
  String service_id;
  dynamic type;

  Providers(this.distro_name, this.service_id, this.type);

  Providers.fromJson(Map<String, dynamic> json) => Providers(
    json['distro_name'],
    json['service_id'],
    json['type'],
 );

}

Main.dart

fetchProviders() async{
    try {
      final response = await http.get(
        uri,
        headers: {'Content-Type': 'application/json','Authorization': 'Bearer ' + _bloc.bearerToken, },
      );
      final responseJson = json.decode(response.body);
      responseJson.forEach((data){
       Providers provider = Providers.fromJson(data);
       providerList.add(provider);
     return responseJson;
    });
    } catch (exception) {
      print(exception);
    }
  }

Upvotes: 0

Related Questions