The syntax of List.iterator property is:
Iterator<E> iterator
This iterator property of List a new Iterator
that allows iterating the elements of this Iterable
.
List.iterator returns value of type Iterator<E>
.
In this example,
numbers
containing integers.Iterator
for the list using the iterator
property.while
loop with moveNext()
to iterate over the list.iter.current
.void main() {
List<int> numbers = [1, 2, 3, 4, 5];
Iterator<int> iter = numbers.iterator;
while (iter.moveNext()) {
print(iter.current);
}
}
1 2 3 4 5
In this example,
characters
containing strings.Iterator
for the set using the iterator
property.while
loop with moveNext()
to iterate over the set.iter.current
.void main() {
Set<String> characters = {'a', 'b', 'c'};
Iterator<String> iter = characters.iterator;
while (iter.moveNext()) {
print(iter.current);
}
}
a b c
In this example,
strings
containing strings.Iterator
for the iterable using the iterator
property.while
loop with moveNext()
to iterate over the iterable.iter.current
.void main() {
Iterable<String> strings = ['apple', 'banana', 'cherry'];
Iterator<String> iter = strings.iterator;
while (iter.moveNext()) {
print(iter.current);
}
}
apple banana cherry
In this Dart tutorial, we learned about iterator property of List: the syntax and few working examples with output and detailed explanation for each example.