Dart Iterator

Summary: in this tutorial, you’ll learn about Dart Iterator<E> interface which is an interface for getting elements from an object.

Introduction to the Dart Iterator<E> class

Dart uses the Iterator<E> interface for getting items, one at a time, from an object.

The Iterable<E> has the moveNext() method that moves the iterator to the next element. It returns true if the object still has at least one element. If no element is left, the moveNext() returns false.

Initially, the iterator is positioned before the first element. Therefore, before accessing the first element (or the next element), you need to call the moveNext() method.

The current property returns the current element. Before accessing the current property, you must always call the moveNext() method and ensure the method returns true.

Note that the for-in statement transparently uses Iterator to test for the end of the iteration and to get each element.

The following example uses an iterator to access elements of a list:

void main() {
  var ratings = [1, 2, 3, 4, 5];
  var it = ratings.iterator;
  while (it.moveNext()) {
    print(it.current);
  }
}Code language: Dart (dart)

Output:

1
2
3
4
5Code language: Dart (dart)

Note that this example is only for demonstration purposes.

How it works.

First, define a list of integers from 1 to 5:

var ratings = [1, 2, 3, 4, 5];Code language: Dart (dart)

Since the ratings is a List<int>, it is also iterable. An iterable has a property iterator that returns an iterator object.

Second, get the iterator from the ratings list:

var it = ratings.iterator;Code language: Dart (dart)

Dart infers the type of it as Iterator<int>.

Third, use a while loop to call the moveNext() to move the iterator to the next element until the last element and display the element in each iteration:

while (it.moveNext()) {
   print(it.current);
}Code language: Dart (dart)

Dart iterator example

The following example defines a class called Sentence that implements the Iterator interface:

class Sentence implements Iterator {
  int _wordIndex = -1;
  List<String> _words = [];

  Sentence(String str) {
    this._words = str.split(' ');
  }

  @override
  get current {
    if (_wordIndex >= 0 && _wordIndex <= _words.length - 1) {
      return _words[_wordIndex];
    }
  }

  @override
  bool moveNext() {
    if (_wordIndex < _words.length - 1) {
      _wordIndex++;
      return true;
    }
    return false;
  }
}

void main() {
  var sentence = Sentence('Dart is awesome');
  while (sentence.moveNext()) {
    print(sentence.current);
  }
}Code language: Dart (dart)

Output:

Dart
is
awesomeCode language: Dart (dart)

How it works.

First, define the Sentence class that implements the Iterator interface. The Sentence class has the following private fields:

  • _words to store words in the input string.
  • _wordIndex to keep track of the index of the current word.

Next, split the input string (str) by space into words in the constructor:

this._words = this._str.split(' ');Code language: Dart (dart)

Then, implement the moveNext() method that increases the _wordindex by one if it is less than the length of the _words list and returns true or false otherwise:

@override
bool moveNext() {
  if (_wordIndex < _words.length - 1) {
    _wordIndex++;
    return true;
  }
  return false;
}Code language: Dart (dart)

After that, implement the current getter that returns a word based on the value of the _wordIndex:

@override
get current {
  if (_wordIndex >= 0 && _wordIndex <= _words.length - 1) {
    return _words[_wordIndex];
  }
}Code language: Dart (dart)

Finally, create a new instance of Sentence and display the words in it using a while loop:

void main() {
  var sentence = Sentence('Dart is awesome');
  while (sentence.moveNext()) {
    print(sentence.current);
  }
}Code language: Dart (dart)

Summary

  • Use Iterator<E> interface for getting items one at a time from an object
Was this tutorial helpful ?