如何在Flutter中从不同类别的产品列表中获取所有产品。

huangapple go评论70阅读模式
英文:

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

问题

我有一个包含产品列表的类别列表。

现在我想展示所有类别中的所有产品。

我该如何像在 Firebase 中那样合并它们...我们有集合组选项...

以下是我的代码:

List<CategoryModel> categories = [
  CategoryModel(id: '1', title: '鞋子', products: [
    Product(id: '1', name: 'Puma', price: 2304.0),
  ]),
  CategoryModel(id: '2', title: '衬衫', products: [
    Product(id: '1', name: 'Spykar', price: 2344.0),
  ]),
  CategoryModel(id: '3', title: '牛仔裤', products: [
    Product(id: '1', name: 'Killer', price: 7838.0),
    Product(id: '2', name: 'Wrangler', price: 12000.0),
  ]),
];

希望这有所帮助!

英文:

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&lt;CategoryModel&gt; categories = [
  CategoryModel(id: &#39;1&#39;, title: &#39;Shoes&#39;, products: [
    Product(id: &#39;1&#39;, name: &#39;Puma&#39;, price: 2304.0),
  ]),
  CategoryModel(id: &#39;2&#39;, title: &#39;Shirts&#39;, products: [
    Product(id: &#39;1&#39;, name: &#39;Spykar&#39;, price: 2344.0),
  ]),
  CategoryModel(id: &#39;3&#39;, title: &#39;Jeans&#39;, products: [
    Product(id: &#39;1&#39;, name: &#39;Killer&#39;, price: 7838.0),
    Product(id: &#39;2&#39;, name: &#39;Wrangler&#39;, price: 12000.0),
  ]),
  
  

];

答案1

得分: 1

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

英文:
categories.expand((element) =&gt; element.products)

答案2

得分: 0

我已经应用了以下代码,它可以工作,但代码不够好看... 你的代码是编码的好方式...

List<Product> productlist=[];

for(int x=0;x<categories.length;x++)
{
  productlist.addAll(categories[x].products);
}
英文:

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

List&lt;Product&gt; productlist=[];

for(int x=0;x&lt;categories.length;x++)
{
  productlist.addAll(categories[x].products);
}

答案3

得分: 0

我认为我理解了你的问题,我有你的问题解决方案,也许这在你的情况下会起作用,尝试按照以下步骤操作:

  1. 创建一个新的列表来存储所有产品。
  2. 遍历每个类别,并将其产品添加到新列表中。
  3. 显示合并后的产品列表。

这里我添加了可能会帮助你解决问题的代码片段:

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 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 &#39;package:flutter/material.dart&#39;;

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&lt;Product&gt; 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&lt;CategoryModel&gt; categories = [
      CategoryModel(id: &#39;1&#39;, title: &#39;Shoes&#39;, products: [
        Product(id: &#39;1&#39;, name: &#39;Puma&#39;, price: 2304.0),
      ]),
      CategoryModel(id: &#39;2&#39;, title: &#39;Shirts&#39;, products: [
        Product(id: &#39;1&#39;, name: &#39;Spykar&#39;, price: 2344.0),
      ]),
      CategoryModel(id: &#39;3&#39;, title: &#39;Jeans&#39;, products: [
        Product(id: &#39;1&#39;, name: &#39;Killer&#39;, price: 7838.0),
        Product(id: &#39;2&#39;, name: &#39;Wrangler&#39;, price: 12000.0),
      ]),
    ];

    List&lt;Product&gt; 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(&#39;Merged Products&#39;),
        ),
        body: ListView.builder(
          itemCount: allProducts.length,
          itemBuilder: (context, index) {
            final product = allProducts[index];
            return ListTile(
              title: Text(product.name),
              subtitle: Text(&#39;Price: ${product.price.toStringAsFixed(2)}&#39;),
            );
          },
        ),
      ),
    );
  }
}

i modified it according to your list

huangapple
  • 本文由 发表于 2023年8月9日 15:45:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865608-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定