length of server response in flutter http package

Issue

I am using future builder for building list view. I am getting the response from server. I am getting exactly what i need. There is single error that i am trying to resolve. The error is when i fetch data from server using http.get method : String data=response.body; I am getting the right response that i want and i am parsing it as String catagoeryId=jsonDecode(data)["data"][i]["_id"];

Now the problem i faced when i decode this json i passed this String inside my constructor using for loop. But i have to give length to stop for loop. the length i am using is: data.length. It decode my json response and passes inside my constructor. But after the json length end its stop working and crashed. I checked the length of data.length its something around 344. But i am having only 3 objects. Here is the code i am using for parsing:

Future<List<ProductCatagoery>> getCatagories()async{
http.Response response = await http.get("http://138.68.134.226:3020/api/category/");
 List<ProductCatagoery> products=[];
  String data=response.body;
  for (int i=0;i<=data.length;i++) {
String catagoeryId=jsonDecode(data)["data"][i]["_id"];
String catagoeryThumb=jsonDecode(data)["data"][i]["thumb"];
String catagoeryName=jsonDecode(data)["data"][i]["name"];
bool catagoeryActive=jsonDecode(data)["data"][i]["active"];


print('name is: $catagoeryId : $catagoeryThumb : $catagoeryName : $catagoeryActive');
  ProductCatagoery newUser= 
  ProductCatagoery(catagoeryId,catagoeryThumb,catagoeryName,catagoeryActive);
  products.add(newUser);
  print('added ${newUser.id}');
  print('length is${products.length}');
  print('last length data: ${data.length}');
 } 
   return products;
 }

Model class:

class ProductCatagoery {
final String id;
 final String thumb;
 final String name;
 final bool active;
  ProductCatagoery(this.id,this.thumb,this.name,this.active);
 }

Response is:

{"success":true,"data":[{"_id":"5f13cc94c63abc03522eff41","thumb":"category/fresh-meat.jpg","name":"Fresh Meat","active":true},{"_id":"5f13cc73c63abc03522eff40","thumb":"category/fruits-vegetables.jpg","name":"Fruits & Vegetables","active":true},{"_id":"5f13cca5c63abc03522eff42","thumb":"category/grocery.jpg","name":"Grocery","active":true}]}

Note: I just need String data=response.body; data length. I am not using an map etc. I also showed products in list if i return product list after 1, 2 or 3th iteration.

Solution

First, decode the response received

final responseFormat = json.decode(response.body);

Then, you can get the list you want to loop with this

final data = responseFormat["data"];

Finally, you can get the length of the list : data.length.

Full code

List<ProductCatagoery> products = [];
  final responseFormat = json.decode(response.body);
  final data = responseFormat["data"];
  for (int i = 0; i < data.length; i++) {
    String catagoeryId = data[i]["_id"];
    String catagoeryThumb = data[i]["thumb"];
    String catagoeryName = data[i]["name"];
    bool catagoeryActive = data[i]["active"];

    print(
        'name is: $catagoeryId : $catagoeryThumb : $catagoeryName : $catagoeryActive');
    ProductCatagoery newUser = ProductCatagoery(
        catagoeryId, catagoeryThumb, catagoeryName, catagoeryActive);
    products.add(newUser);
    print('added ${newUser.id}');
    print('length is${products.length}');
    print('last length data: ${data.length}');
  }

Answered By – Lapa Ny Aina Tanjona

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *