在flutter中解析JsonData时发生意外错误

时间:2020-03-23 15:57:02

标签: json flutter dart get

正在尝试解析json列表数据,以便将其保存到对象中。但是我每次都会出错,我也不知道为什么。

Json数据

{
    "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"
            ]
        }
    ]
}

这是我的数据模型类。

模型

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);
    }
  }

这是我得到的错误。

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

我真的不知道为什么会出现此错误。我的api上没有空值,过去我已经使用了这种json结构。

3 个答案:

答案 0 :(得分:1)

初始化providerList

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

使用列表图:

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

答案 1 :(得分:0)

模型

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);
    }
  }

答案 2 :(得分:0)

模型

       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);
    }
  }