扑。按列表字段分组的列表
Flutter. Grouped list by a field which is a list
我有一个包含字段组的项目列表。组也是列表。我想显示按组分组的项目列表。该项目可以是两个或多个组的一部分。
List<Product> _items = [
Product(
id: '1',
title: 'Apple and more more thing', description: 'Some data to describe',
group: ['Picnic', 'New'], price: 25, bonus: 1, quantity: 1.5, measure: 'кг', measureStep: 1.5,
imageUrl: 'assets/fruits/orange.jpg'),
Product(
id: '2',
title: 'Apple and more more thing', description: 'Some data to describe',
price: 24.50, bonus: 1, group: ['Picnic', 'New'], quantity: 1.5, measure: 'кг', measureStep: 1.5,
imageUrl: 'assets/fruits/orange.jpg'),
Product(
id: '3',
title: 'Apple and more more thing', description: 'Some data to describe',
price: 5.00, bonus: 1, group: ['New'], quantity: 1, measure: 'шт', measureStep: 1,
imageUrl: 'assets/fruits/orange.jpg'),
Product(
id: '4',
title: 'Apple and more more thing', description: 'Some data to describe',
price: 24.00, bonus: 1, group: ['New'], quantity: 1.250, measure: 'кг', measureStep: 1.250,
imageUrl: 'assets/fruits/orange.jpg'),
Product(
id: '5',
title: 'Apple and more more thing', description: 'Some data to describe',
price: 15.75, bonus: 1, group: ['Picnic'], quantity: 1, measure: 'шт', measureStep: 1,
imageUrl: 'assets/fruits/orange.jpg'),
Product(
id: '6',
title: 'Apple and more more thing', description: 'Some data to describe',
price: 9.50, bonus: 1, group: ['For children', 'New'], quantity: 0.5, measure: 'лт', measureStep: 0.5,
imageUrl: 'assets/fruits/orange.jpg'),
Product(
id: '7',
title: 'Apple and more more thing', description: 'Some data to describe',
price: 40.00, bonus: 1, group: ['For children'], quantity: 1, measure: 'шт', measureStep: 1,
imageUrl: 'assets/fruits/orange.jpg'),
Product(
id: '8',
title: 'Apple and more more thing', description: 'Some data to describe',
price: 32.50, bonus: 1, group: ['For children', 'New'], quantity: 1.75, measure: 'кг', measureStep: 1.75,
imageUrl: 'assets/fruits/orange.jpg')
我是 flutter 的新手,所以你能告诉我它是如何实现的或者代码会很完美吗?
这是你想要的吗?
var list = [
Product(id: '1', groups: ['Picnic', 'New']),
Product(id: '2', groups: ['For children', 'New']),
Product(id: '3', groups: ['New']),
Product(id: '4', groups: ['For children']),
];
var groupedLists = {};
list.forEach((product) {
product.groups.forEach((group) {
if (groupedLists['$group'] == null) {
groupedLists['$group'] = <Product>[];
}
(groupedLists['$group'] as List<Product>).add(product);
});
});
print(groupedLists);
结果是:
{
Picnic: [Instance of 'Product'],
New: [Instance of 'Product', Instance of 'Product', Instance of 'Product'],
For children: [Instance of 'Product', Instance of 'Product']
}
现在您可以轻松地在 LisView
中显示它们
我有一个包含字段组的项目列表。组也是列表。我想显示按组分组的项目列表。该项目可以是两个或多个组的一部分。
List<Product> _items = [
Product(
id: '1',
title: 'Apple and more more thing', description: 'Some data to describe',
group: ['Picnic', 'New'], price: 25, bonus: 1, quantity: 1.5, measure: 'кг', measureStep: 1.5,
imageUrl: 'assets/fruits/orange.jpg'),
Product(
id: '2',
title: 'Apple and more more thing', description: 'Some data to describe',
price: 24.50, bonus: 1, group: ['Picnic', 'New'], quantity: 1.5, measure: 'кг', measureStep: 1.5,
imageUrl: 'assets/fruits/orange.jpg'),
Product(
id: '3',
title: 'Apple and more more thing', description: 'Some data to describe',
price: 5.00, bonus: 1, group: ['New'], quantity: 1, measure: 'шт', measureStep: 1,
imageUrl: 'assets/fruits/orange.jpg'),
Product(
id: '4',
title: 'Apple and more more thing', description: 'Some data to describe',
price: 24.00, bonus: 1, group: ['New'], quantity: 1.250, measure: 'кг', measureStep: 1.250,
imageUrl: 'assets/fruits/orange.jpg'),
Product(
id: '5',
title: 'Apple and more more thing', description: 'Some data to describe',
price: 15.75, bonus: 1, group: ['Picnic'], quantity: 1, measure: 'шт', measureStep: 1,
imageUrl: 'assets/fruits/orange.jpg'),
Product(
id: '6',
title: 'Apple and more more thing', description: 'Some data to describe',
price: 9.50, bonus: 1, group: ['For children', 'New'], quantity: 0.5, measure: 'лт', measureStep: 0.5,
imageUrl: 'assets/fruits/orange.jpg'),
Product(
id: '7',
title: 'Apple and more more thing', description: 'Some data to describe',
price: 40.00, bonus: 1, group: ['For children'], quantity: 1, measure: 'шт', measureStep: 1,
imageUrl: 'assets/fruits/orange.jpg'),
Product(
id: '8',
title: 'Apple and more more thing', description: 'Some data to describe',
price: 32.50, bonus: 1, group: ['For children', 'New'], quantity: 1.75, measure: 'кг', measureStep: 1.75,
imageUrl: 'assets/fruits/orange.jpg')
我是 flutter 的新手,所以你能告诉我它是如何实现的或者代码会很完美吗?
这是你想要的吗?
var list = [
Product(id: '1', groups: ['Picnic', 'New']),
Product(id: '2', groups: ['For children', 'New']),
Product(id: '3', groups: ['New']),
Product(id: '4', groups: ['For children']),
];
var groupedLists = {};
list.forEach((product) {
product.groups.forEach((group) {
if (groupedLists['$group'] == null) {
groupedLists['$group'] = <Product>[];
}
(groupedLists['$group'] as List<Product>).add(product);
});
});
print(groupedLists);
结果是:
{
Picnic: [Instance of 'Product'],
New: [Instance of 'Product', Instance of 'Product', Instance of 'Product'],
For children: [Instance of 'Product', Instance of 'Product']
}
现在您可以轻松地在 LisView