To iterate over a set in Dart, you can use the for loop or the forEach method.
In this example,
void main() {
// Define set
Set<String> mySet = {'apple', 'banana', 'orange'};
// Iterate over set of strings using for loop
for (String item in mySet) {
print(item);
}
}
apple banana orange
In this example,
void main() {
// Define set
Set<int> mySet = {1, 2, 3, 4, 5};
// Iterate over set of numbers using forEach method
mySet.forEach((item) {
print(item);
});
}
1 2 3 4 5
In this tutorial, we learned How to Iterate Over a Set in Dart language with well detailed examples.