CachingIterable<E> constructor Null safety

  1. Iterator<E> _prefillIterator

Creates a CachingIterable using the given Iterator as the source of data. The iterator must be non-null and must not throw exceptions.

Since the argument is an Iterator, not an Iterable, it is guaranteed that the underlying data set will only be walked once. If you have an Iterable, you can pass its iterator field as the argument to this constructor.

You can use a sync* function with this as follows:

Iterable<int> range(int start, int end) sync* {
  for (int index = start; index <= end; index += 1)
    yield index;

Iterable<int> i = CachingIterable<int>(range(1, 5).iterator);
print(i.length); // walks the list
print(i.length); // efficient