skip to Main Content

I have the following List of Map 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"
  }
];

I want to remove the duplicate items from this List. How can I do that in Flutter?

P.S. – .toSet() and .toList() methods did not work in this case.

4

Answers


  1. Suppose you store the entire list in a List l1=[. . .], use toSet() for removing duplicates:

    l1.toSet().toList();
    
    Login or Signup to reply.
  2. If it’s sufficient to only check on equal userIDs you could do this:

    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();
    }
    
    Login or Signup to reply.
  3. Write a method which will filter items by specific field occurrence in 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();
    }
    

    and use it:

    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);
      
    }
    

    You can test it here

    Login or Signup to reply.
  4. One way to remove duplicates from a List of Map objects in Flutter without using a set is by iterating over the List and adding each Map object to a new List only if it does not already exist in the new 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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search