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
Suppose you store the entire list in a List l1=[. . .], use toSet() for removing duplicates:
If it’s sufficient to only check on equal
userID
s you could do this:Write a method which will filter items by specific field occurrence in
Set
:and use it:
You can test it here
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.