toMap<E> method

Map<T, E> toMap<E>(
  1. E mapper(
    1. T element
    )
)

Converts the list into a map where each element of the list is a key, and the value is generated by applying the mapper function to each element.

Example usage:

void main() {
  List<String> fruits = ['apple', 'banana', 'orange'];

  Map<String, int> fruitLengthMap = fruits.toMap((fruit) => fruit.length);
  print(fruitLengthMap); // Output: {apple: 5, banana: 6, orange: 6}

  Map<String, String> uppercaseMap = fruits.toMap((fruit) => fruit.toUpperCase());
  print(uppercaseMap); // Output: {apple: APPLE, banana: BANANA, orange: ORANGE}
}

Implementation

Map<T, E> toMap<E>(E Function(T element) mapper) => {for (final key in this) key: mapper(key)};