toUnmodifiableMap<K, V> method
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,
});