CachingIterable<E> constructor Null safety

CachingIterable<E>(
  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 this with an existing sync* function 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

Beware that this will eagerly evaluate the range iterable, and because of that it would be better to just implement range as something that returns a List to begin with if possible.

Implementation

CachingIterable(this._prefillIterator);