mapWithIndex<E> method

Iterable<E> mapWithIndex<E>(
  1. E f(
    1. int index,
    2. T value
    )
)

Maps each element of the iterable along with its index using the provided function f.

Example usage:

void main() {
  const List<String> fruits = ['Apple', 'Banana', 'Orange', 'Mango'];

  final indexedFruits = fruits.mapWithIndex((index, fruit) => '$index: $fruit').toList();

  indexedFruits.forEach(print);
}

Implementation

Iterable<E> mapWithIndex<E>(E Function(int index, T value) f) =>
    Iterable.generate(length).map((i) => f(i, elementAt(i)));