英文:
how to get all product from a list of different categories in flutter
问题
我有一个包含产品列表的类别列表。
现在我想要显示所有类别中的所有产品。
如何像在Firebase中那样合并呢?我们在collectionGroup中有选项...
以下是我的代码:
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<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),
]),
];
答案1
得分: 1
categories.expand((element) => element.products)的翻译结果是:扩展categories,对每个元素执行(element) => element.products的操作。
英文:
categories.expand((element) => 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<Product> productlist=[];
for(int x=0;x<categories.length;x++)
{
productlist.addAll(categories[x].products);
}
答案3
得分: 0
我认为我理解了你的问题,并且我有一个解决方案,也许在你的情况下会起作用,请尝试按照以下步骤进行操作:
- 创建一个新的列表来存储所有的产品。
- 遍历每个类别,并将其产品添加到新的列表中。
- 显示合并后的产品列表。
以下是我添加的代码片段,希望能帮助你解决问题:
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: '鞋子', 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),
]),
];
List<Product> allProducts = [];
// 将所有类别的产品合并到一个列表中
for (var category in categories) {
allProducts.addAll(category.products);
}
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('合并后的产品'),
),
body: ListView.builder(
itemCount: allProducts.length,
itemBuilder: (context, index) {
final product = allProducts[index];
return ListTile(
title: Text(product.name),
subtitle: Text('价格:${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:
- Create a new list to store all products.
- Iterate through each category and add its products to the new list.
- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论