Home > database >  Flutter How to convert Object of Object to List
Flutter How to convert Object of Object to List

Time:11-17

How can I make a list of prices that I got from my API.

{
  product: {
     items: {
        price1: {}
        price2: {}
        price3: {}
     }
  }
}
 var productPrices = response.data

I have tried this one to get the prices but I would like a help to convert it to a List so I can map it to my widget, what I get in the list of Items is _JsonMap.

  productPrices.forEach((key, value) {
    print(key);
    final Map listofItems = Map.from(value);
    print(listofItems);
  });

CodePudding user response:

Usually, serialization is recommended, but if you want, you can quickly access the products as follows.

(jsonDecode(data)['product']['items'] as Map).values.toList()

CodePudding user response:

You are missing .items

Try:

productPrices.items.forEach((key, value) {
    print(key);
    final Map listofItems = Map.from(value);
    print(listofItems);
  });

CodePudding user response:

First of all it's better to create a class for product or which model you need. You can use this site to generate model from json. https://javiercbk.github.io/json_to_dart/

After then you can use fromJson() method to generate object from json.

  • Related