Home > database >  Flutter : Range error on accessing JSON Response
Flutter : Range error on accessing JSON Response

Time:04-18

I am accessing a JSON response with the following structure.

{
    "fullName": "FirstName LastName",
    "listings": "5",
    "items": [{
        "type": "A",
        "id": "xi0823109y",
        "imgUrl": "imageurl.com/logo/png",
        "addressListings": [{
                "type": "residential",
                "amount": "790909"
            },
            {
                "type": "commerical",
                "amount": "9808212"
            }
        ]

    }]


}

I am able to display all data in my ListView, except for certain conditions where addressListings may have 1 or 2 object, meaning it can have either or both Residential and Commercial and I want only the value for amount. I am having trouble putting a condition to check if any of the items are present, there is always 1 item (Residential or Commercial). When i try to do a null check it gives me range error.

Here is my model

class CusomerDetails {
  final String fullName;
  final String listings;
  final String type;
  final String imgUrl;
  final String id;
  final String? amount;

  CusomerDetails({
    required this.fullName,   
    required this.listings,
    required this.type,
    required this.imgUrl,
    required this.id,
     this.amount,
  });
}

This is the function where I am trying to get the data and pass it to ListView builder

    Class GetCustomerData{
    Future<List<CustomerDetails>> fetchData(queryStr) async {
    try{
    var response = await http.get(Uri,parse(apiurl queryStr);
    if(reponse.statusCode == 200){
    return _parseResponse(response.body);
    }else{
    print('someError'}
    }
    on TimeoutException catch (_) {
          print('Timeout Error');
        }
        throw {};
    }
    }
    
    List<CustomerDetails> _parseResponse(String jsonObj){
    final jsonMapObj = jsonDecode(jsonObj);
    final list = (jsonMapObj['items'] as list);
    var obj = list.map((map)=> CustomerDetails)(
    id:map['id'],
    fullname:map['fullName'],
    type:map['type'],
    imgUrl:map[imgUrl],
    amount:map['addressListings']['0']?['amount'] ?? 'Not Available',
  //  amount1:map['addressListings']['1']?['amount'] ?? 'Not Available' // tried this way out but didn't work so commented for now.
    )).toList()
    return obj;
    }
    }

Lastly if the Amount is not present I am trying to pass "Not Available" however I am yet to reach that, because of the range error.

CodePudding user response:

Class GetCustomerData{
Future<List<CustomerDetails>> fetchData(queryStr) async {
  try{
    var response = await http.get(Uri,parse(apiurl queryStr);
        if(reponse.statusCode == 200){
      return _parseResponse(response.body);
    }else{
  print('someError'}
  }
  on TimeoutException catch (_) {
  print('Timeout Error');
  }
  throw {};
}
}

List<CustomerDetails> _parseResponse(String jsonObj){
  final jsonMapObj = jsonDecode(jsonObj);
  final list = (jsonMapObj['items'] as list);
  var obj = list.map((map)=> CustomerDetails)(
      id:map['id'],
      fullname:map['fullName'],
      type:map['type'],
      imgUrl:map[imgUrl],
      amount:map['addressListings'].length<1?'Not Available':map['addressListings']['0']['amount'] ?? 'Not Available',
      amount1:map['addressListings'].length<2? 'Not Available':map['addressListings']['1']['amount'] ?? 'Not Available' // tried this way out but didn't work so commented for now.
  )).toList()
  return obj;
}
}
  • Related