Dart – Iterate over Elements of Set using For-in

To iterate over elements of a Set in Dart, we can use for-in statement. During each iteration, an element can be accessed inside the for loop.

Syntax

The syntax to iterate over elements of Set mySet using For-in statement is

for (var e in mySet) {
  //code
}

Examples

In the following program, we take a Set mySet, iterate over the elements using for-in statement, and print each element to console.

main.dart

void main() {
  Set mySet = {'apple', 'banana', 'cherry'};
  for (var e in mySet) {
    print(e);
  }
}

Output

apple
banana
cherry

Conclusion

In this Dart Tutorial, we learned how to iterate over the elements of a Set in Dart language, with examples.