Flutter dropdown can't have duplicate values? But unfortunately I have those as it is fetching from an API

Viewed 140

My dropdown is having duplicate values as it is fetching Pincode data from an API. Can someone help me to fix this error as the dropdown cant have duplicate values? How to stop duplicate values while fetching the data from the API?enter image description here

2 Answers

You can convert your list to set and then again back to list for to delete duplicated values. Set doesnt contain duplicated values. For more check out documentation of set

void main() {
final myList = [
{
  'a': 'apple',
  'name':'sam',
  'age': 41,
},
{
  'a': 'apple',
  'name':'alex',
  'age': 43,
},
{
  'a': 'ban',
  'name':'robby',
  'age': 41,
}
];
var uniqueIds = myList.map((o) => o["a"]).toSet().toList();

print(uniqueIds);
}

Mapping the list with specific key and then => conversion of toSet() and toList() solves my problem

Related