英文:
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<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.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: '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:
- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论