flutter 正在从Dart中的重复项列表中获取唯一对象列表

v6ylcynt  于 2023-03-19  发布在  Flutter
关注(0)|答案(5)|浏览(244)

我有以下Map对象列表:

[
  {
    "name": "Sameera Tennakoon",
    "userID": "7c770f8d-hju4-4ddd-b5cb-f3d0caba2bb3",
    "type": "CASHIER"
  },
  {
    "name": "Sam Perera",
    "userID": "7c770f8d-f6f4-3eee-b5cb-f3d0caba2bb3",
    "type": "CASHIER"
  },
  {
    "name": "Uma Jackson",
    "userID": "7c770f8d-f6f4-4ddd-5634-f3d0caba2bb3",
    "type": "CASHIER"
  },
  {
    "name": "Anu Ima",
    "userID": "7c770f8d-f6f4-4ddd-lksa-f3d0caba2bb3",
    "type": "CASHIER"
  },
  {
    "name": "Sameera Tennakoon",
    "userID": "7c770f8d-hju4-4ddd-b5cb-f3d0caba2bb3",
    "type": "CASHIER"
  }
];

我想从这个列表中删除重复的项目。我该如何在Flutter中做到这一点?
P.S. -.toSet().toList()方法在这种情况下不起作用。

euoag5mw

euoag5mw1#

假设您将整个列表存储在List l1=[...]中,使用toSet()删除重复项:

l1.toSet().toList();
8oomwypt

8oomwypt2#

如果只检查相等的userID s就足够了,可以这样做:

import 'package:collection/collection.dart';

void main(){
  final list = [
    {
      "name": "Sameera Tennakoon",
      "userID": "7c770f8d-hju4-4ddd-b5cb-f3d0caba2bb3",
      "type": "CASHIER"
    },
    {
      "name": "Sam Perera",
      "userID": "7c770f8d-f6f4-3eee-b5cb-f3d0caba2bb3",
      "type": "CASHIER"
    },
    {
      "name": "Uma Jackson",
      "userID": "7c770f8d-f6f4-4ddd-5634-f3d0caba2bb3",
      "type": "CASHIER"
    },
    {
      "name": "Anu Ima",
      "userID": "7c770f8d-f6f4-4ddd-lksa-f3d0caba2bb3",
      "type": "CASHIER"
    },
    {
      "name": "Sameera Tennakoon",
      "userID": "7c770f8d-hju4-4ddd-b5cb-f3d0caba2bb3",
      "type": "CASHIER"
    }
  ];

  final result = groupBy(list, (Map e) => e['userID']).values.map((e) => e.first).toList();
}
atmip9wb

atmip9wb3#

编写一个方法,通过Set中特定字段的出现来过滤项目:

List<Map<String, dynamic>> filterListByKey (
  List<Map<String, dynamic>> list,
  String key
) {
  Set ids = Set<String>();
  
  return list.where((e) {
    if (ids.contains(e[key].toString())) {
      return false;
    }
    
    ids.add(e[key].toString());
    return true;
  }).toList();
}

并使用它:

void main() {
  final List<Map<String, dynamic>> users = [
    {
      "name": "Sameera Tennakoon",
      "userID": "7c770f8d-hju4-4ddd-b5cb-f3d0caba2bb3",
      "type": "CASHIER"
    },
    {
      "name": "Sam Perera",
      "userID": "7c770f8d-f6f4-3eee-b5cb-f3d0caba2bb3",
      "type": "CASHIER"
    },
    {
      "name": "Uma Jackson",
      "userID": "7c770f8d-f6f4-4ddd-5634-f3d0caba2bb3",
      "type": "CASHIER"
    },
    {
      "name": "Anu Ima",
      "userID": "7c770f8d-f6f4-4ddd-lksa-f3d0caba2bb3",
      "type": "CASHIER"
    },
    {
      "name": "Sameera Tennakoon",
      "userID": "7c770f8d-hju4-4ddd-b5cb-f3d0caba2bb3",
      "type": "CASHIER"
    }
  ];
  final uniqueUsers = filterListByKey(users, 'userID');
  
  print(uniqueUsers);
  
}

你可以在这里测试

vnzz0bqm

vnzz0bqm4#

请参阅HashSet的文档。
HashSet的元素必须具有一致的equality和hashCode实现,这意味着equals操作必须在元素上定义稳定的等价关系(自反、对称、传递和随时间的一致性),并且hashCode必须与equality保持一致,以便对于被视为相等的对象是相同的。
Map对象不满足上述要求,换句话说,以下代码将打印false

print({'a': 1} == {'a': 1});

您可以通过创建一个Set来解决这个问题,该Set使用equalshashCode的自定义行为,如下所示:

import 'dart:collection';

void main() {
  List<Map<String, String>> objects = [
    {
      "name": "Sameera Tennakoon",
      "userID": "7c770f8d-hju4-4ddd-b5cb-f3d0caba2bb3",
      "type": "CASHIER"
    },
    {
      "name": "Sam Perera",
      "userID": "7c770f8d-f6f4-3eee-b5cb-f3d0caba2bb3",
      "type": "CASHIER"
    },
    {
      "name": "Uma Jackson",
      "userID": "7c770f8d-f6f4-4ddd-5634-f3d0caba2bb3",
      "type": "CASHIER"
    },
    {
      "name": "Anu Ima",
      "userID": "7c770f8d-f6f4-4ddd-lksa-f3d0caba2bb3",
      "type": "CASHIER"
    },
    {
      "name": "Sameera Tennakoon",
      "userID": "7c770f8d-hju4-4ddd-b5cb-f3d0caba2bb3",
      "type": "CASHIER"
    }
  ];
  
  Set<Map<String, String>> unique = HashSet(
    equals: (a, b) => a['userID'] == b['userID'],
    hashCode: (a) => a['userID'].hashCode,
  )..addAll(objects);
  
  unique.forEach(print);
}
gpnt7bae

gpnt7bae5#

在Flutter中不使用集合从Map对象的List中删除重复项的一种方法是迭代List,并且仅当每个Map对象在新List中不存在时,才将其添加到新List中。

final List<Map<String, dynamic>> originalList = [...];
final List<Map<String, dynamic>> newList = [];

originalList.forEach((map) {
  if (!newList.any((newMap) => newMap['userID'] == map['userID'])) {
    newList.add(map);
  }
});

print(newList);

相关问题