toUnmodifiableMap<K, V> method

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

Returns an unmodifiable map using create to produce entries from elements in this iterable.

Earlier entries are replaced by later entries if they contain the same key. It 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 toMap for creating a modifiable Map.

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

Implementation

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