skip to Main Content

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),
  ]),
  
  

];

3

Answers


  1. Chosen as BEST ANSWER

    I had applied following it works but not looking good code..Your code is good way of coding..

    List<Product> productlist=[];
    
      for(int x=0;x<categories.length;x++)
        {
          productlist.addAll(categories[x].products);
        }
    
     
    

  2. categories.expand((element) => element.products)
    
    Login or Signup to reply.
  3. i think i understand your problem and i have your problem solution may be this gonna workout in your scenario try to follow these steps:

    1. Create a new list to store all products.
    2. Iterate through each category and add its products to the new list.
    3. Display the merged list of products.

    here i am adding my code snippet which may help you out in your query

    import 'package:flutter/material.dart';
    
    class Product {
      final String id;
      final String name;
      final double price;
    
      Product({required this.id, required this.name, required this.price});
    }
    
    class CategoryModel {
      final String id;
      final String title;
      final List<Product> products;
    
      CategoryModel({required this.id, required this.title, required this.products});
    }
    
    void main() {
      runApp(MyApp());
    }
    
    class Groccerapp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        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),
          ]),
        ];
    
        List<Product> allProducts = [];
    
        // Merge products from all categories into a single list
        for (var category in categories) {
          allProducts.addAll(category.products);
        }
    
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text('Merged Products'),
            ),
            body: ListView.builder(
              itemCount: allProducts.length,
              itemBuilder: (context, index) {
                final product = allProducts[index];
                return ListTile(
                  title: Text(product.name),
                  subtitle: Text('Price: ${product.price.toStringAsFixed(2)}'),
                );
              },
            ),
          ),
        );
      }
    }
    

    i modified it according to your list

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search