Iterable<E>.withIterator constructor

  1. @Since("3.8")
Iterable<E>.withIterator(
  1. Iterator<E> iteratorFactory()
)

Creates an Iterable from the Iterator factory.

The returned iterable creates a new iterator each time iterator is read, by calling the provided iteratorFactory function. The iteratorFactory function must return a new instance of Iterator<E> on each call.

This factory is useful when you need to create an iterable from a custom iterator, or when you want to ensure a fresh iteration state on each use.

Example:

final numbers = Iterable.withIterator(() => [1, 2, 3].iterator);
print(numbers.toList()); // [1, 2, 3]

Implementation

@Since("3.8")
factory Iterable.withIterator(Iterator<E> Function() iteratorFactory) =
    _WithIteratorIterable<E>;