toIterable method

Iterable<T> toIterable()

Returns a new lazy Iterable containing all elements of this collection.

This method uses a generator (sync*) to yield each element of the current collection. It's useful for converting collections like Set or List to Iterable with lazy evaluation.

  • Returns: A new lazy Iterable containing all elements of the current collection.

  • Examples:

    List<int> numbers = [1, 2, 3];
    Iterable<int> iterableNumbers = numbers.toIterable();
    print(iterableNumbers); // Iterable containing 1, 2, 3
    

Note: The returned Iterable is lazy, meaning it only processes elements as they are iterated over. This is useful for potentially large collections where you want to avoid immediate evaluation.

Implementation

Iterable<T> toIterable() sync* {
  yield* this;
}