associateWith<V> method

Map<T, V> associateWith<V>(
  1. V valueSelector(
    1. T element
    )
)

Returns a map where every element is used as a key that is associated with a value produced by the valueSelector function.

Example:

[1, 2, 3].associateWith((e) => e * 1000); // {1: 1000, 2: 2000, 3: 3000}

Implementation

Map<T, V> associateWith<V>(V Function(T element) valueSelector) {
  ArgumentError.checkNotNull(valueSelector, 'valueSelector');
  final map = <T, V>{};
  forEach((element) {
    map[element] = valueSelector(element);
  });
  return map;
}