toMap<K, V> method

  1. @useResult
Map<K, V> toMap<K, V>(
  1. (K, V) create(
    1. E element
    )
)

Returns a modifiable map using create to produce entries from elements in this iterable.

Earlier entries are replaced by later entries if they contain the same key.

This function is an alternative to map comprehension when there are multiple steps in producing entries that cannot be expressed in a single expression.

This function is non-deterministic when this iterable is unordered, i.e. HashSet.

See toUnmodifiableMap for creating a unmodifiable Map.

['a', 'b', 'c'].toMap((element) {
  final foo = someFunction(element);
  return someOtherFunction(foo) ?? yetAnotherFunction(foo);
});

Implementation

@useResult Map<K, V> toMap<K, V>((K, V) Function(E element) create) => {
  for (final (key, value) in map((e) => create(e)))
    key: value,
};