To remove duplicates in a list in Dart, you can use the toSet method followed by the toList method.
In this example,
numbers
with integer values.void main() {
List<int> numbers = [1, 2, 2, 3, 4, 4, 5];
List<int> distinctNumbers = numbers.toSet().toList();
print('List without duplicates: $distinctNumbers');
}
List without duplicates: [1, 2, 3, 4, 5]
In this example,
words
with string values.void main() {
List<String> words = ['apple', 'banana', 'cherry', 'apple', 'banana'];
List<String> distinctWords = words.toSet().toList();
print('List without duplicates: $distinctWords');
}
List without duplicates: [apple, banana, cherry]
In this tutorial, we learned How to Remove Duplicates in a List in Dart language with well detailed examples.