flutterdart

how to get all product from a list of different categories in flutter


I have list of category that contains list of products..

Now i want to show all products from this all categories

How can i merge like in firebase ..we have option for collectionGroup...

here is my code



List<CategoryModel> categories = [
  CategoryModel(id: '1', title: 'Shoes', products: [
    Product(id: '1', name: 'Puma', price: 2304.0),
  ]),
  CategoryModel(id: '2', title: 'Shirts', products: [
    Product(id: '1', name: 'Spykar', price: 2344.0),
  ]),
  CategoryModel(id: '3', title: 'Jeans', products: [
    Product(id: '1', name: 'Killer', price: 7838.0),
    Product(id: '2', name: 'Wrangler', price: 12000.0),
  ]),
  
  

];

Solution

  • categories.expand((element) => element.products)