'Json response management in Flutter

I am calling and API and getting json data in response. For the Successful response, i.e. If there is data in response, I am getting response aas below :

{"data":{"name":"DOE,JOHN SMITH","clientId":"NaN","bvn":"12212345229","account":{"id":"444116190000000000000457874293","number":"1234567890"},"status":"2","currency":"OMN","bank":"ODBank"}}

But, If there is no data match for my passed parameters on server I am getting empty json data as below :

{"data":[]}

Now, Model class I have created is as below :

  class ResAccountNumberDetails {
  Data data;

  ResAccountNumberDetails({this.data});

  ResAccountNumberDetails.fromJson(Map<String, dynamic> json) {
    data = json['data'] != null ? new Data.fromJson(json['data']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.data != null) {
      data['data'] = this.data.toJson();
    }
    return data;
  }
}

class Data {
  String name;
  String clientId;
  String bvn;
  Account account;
  String status;
  String currency;
  String bank;

  Data(
      {this.name,
        this.clientId,
        this.bvn,
        this.account,
        this.status,
        this.currency,
        this.bank});

  Data.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    clientId = json['clientId'];
    bvn = json['bvn'];
    account =
    json['account'] != null ? new Account.fromJson(json['account']) : null;
    status = json['status'];
    currency = json['currency'];
    bank = json['bank'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    data['clientId'] = this.clientId;
    data['bvn'] = this.bvn;
    if (this.account != null) {
      data['account'] = this.account.toJson();
    }
    data['status'] = this.status;
    data['currency'] = this.currency;
    data['bank'] = this.bank;
    return data;
  }
}

class Account {
  String id;
  String number;

  Account({this.id, this.number});

  Account.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    number = json['number'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['number'] = this.number;
    return data;
  }
}

Parsing response as below :

 Future<ResAccountNumberDetails> accountNumberDetails(
      ReqAccountNumberDetails reqAccountNumberDetails) async {
    try {
      final response = await ApiService().get(ApiConstants
              .apiAccountNumberDetails +
          "?accountNumber=${reqAccountNumberDetails.accountNumber}&bankCode=${reqAccountNumberDetails.bankCode}");
      return ResAccountNumberDetails.fromJson(response.data);
    } on DioError catch (error) {
      throw ResBaseModel.fromJsonWithCode(error.response);
    }
  }

Now, As you can see above in json response when there is empty data or no data in response, I am getting [] Array for data key, instead of {} object key.

So, How can I check that there is no data and can update my UI ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source