扑。按列表字段分组列表

Nur*_*ulu 0 list slug dart flutter grouped-list

我有一个带有字段组的项目列表。组也是列表。我想显示按组分组的项目列表。该项目可以是两个或多个组的一部分。

\n
List<Product> _items = [\n    Product(\n        id: \'1\',\n        title: \'Apple and more more thing\', description: \'Some data to describe\',\n        group: [\'Picnic\', \'New\'], price: 25, bonus: 1, quantity: 1.5, measure: \'\xd0\xba\xd0\xb3\', measureStep: 1.5,\n        imageUrl: \'assets/fruits/orange.jpg\'),\n    Product(\n        id: \'2\',\n        title: \'Apple and more more thing\', description: \'Some data to describe\',\n        price: 24.50, bonus: 1, group: [\'Picnic\', \'New\'], quantity: 1.5, measure: \'\xd0\xba\xd0\xb3\', measureStep: 1.5,\n        imageUrl: \'assets/fruits/orange.jpg\'),\n    Product(\n        id: \'3\',\n        title: \'Apple and more more thing\', description: \'Some data to describe\',\n        price: 5.00, bonus: 1, group: [\'New\'], quantity: 1, measure: \'\xd1\x88\xd1\x82\', measureStep: 1,\n        imageUrl: \'assets/fruits/orange.jpg\'),\n    Product(\n        id: \'4\',\n        title: \'Apple and more more thing\', description: \'Some data to describe\',\n        price: 24.00, bonus: 1, group: [\'New\'], quantity: 1.250, measure: \'\xd0\xba\xd0\xb3\', measureStep: 1.250,\n        imageUrl: \'assets/fruits/orange.jpg\'),\n    Product(\n        id: \'5\',\n        title: \'Apple and more more thing\', description: \'Some data to describe\',\n        price: 15.75, bonus: 1, group: [\'Picnic\'], quantity: 1, measure: \'\xd1\x88\xd1\x82\', measureStep: 1,\n        imageUrl: \'assets/fruits/orange.jpg\'),\n    Product(\n        id: \'6\',\n        title: \'Apple and more more thing\', description: \'Some data to describe\',\n        price: 9.50, bonus: 1, group: [\'For children\', \'New\'], quantity: 0.5, measure: \'\xd0\xbb\xd1\x82\', measureStep: 0.5,\n        imageUrl: \'assets/fruits/orange.jpg\'),\n    Product(\n        id: \'7\',\n        title: \'Apple and more more thing\', description: \'Some data to describe\',\n        price: 40.00, bonus: 1, group: [\'For children\'], quantity: 1, measure: \'\xd1\x88\xd1\x82\', measureStep: 1,\n        imageUrl: \'assets/fruits/orange.jpg\'),\n    Product(\n        id: \'8\',\n        title: \'Apple and more more thing\', description: \'Some data to describe\',\n        price: 32.50, bonus: 1, group: [\'For children\', \'New\'], quantity: 1.75, measure: \'\xd0\xba\xd0\xb3\', measureStep: 1.75,\n        imageUrl: \'assets/fruits/orange.jpg\')\n
Run Code Online (Sandbox Code Playgroud)\n

我是颤振的新手,所以你能告诉我这是如何实现的吗?或者代码将是完美的。

\n

我按一个字符串创建了分组列表,但现在我有一个列表。

\n

小智 5

这是你想要的吗?

  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);
Run Code Online (Sandbox Code Playgroud)

结果是:

{
Picnic: [Instance of 'Product'], 
New: [Instance of 'Product', Instance of 'Product', Instance of 'Product'], 
For children: [Instance of 'Product', Instance of 'Product']
}
Run Code Online (Sandbox Code Playgroud)

现在您可以轻松地将它们显示在LisView