StreamMap<K, V>.fromIterable constructor
- Iterable iterable, {
- _Compute<
K> ? key, - _Compute<
V> ? value, - OnUpdate<
Map< ? onUpdate,K, V> > - OnEvent<
CollectionEvent< ? onEvent,K, V> > - OnChange<
CollectionChangeEvent< ? onChange,K, V> >
Creates a StreamMap in which the keys and values are computed from the
iterable
.
The created map is a LinkedHashMap.
A LinkedHashMap
requires the keys to implement compatible
operator==
and hashCode
, and it allows null as a key.
It iterates in key insertion order.
For each element of the iterable
this constructor computes a key/value
pair, by applying key
and value
respectively.
The example below creates a new Map from a List. The keys of map
are
list
values converted to strings, and the values of the map
are the
squares of the list
values:
List<int> list = [1, 2, 3];
Map<String, int> map = Map.fromIterable(list,
key: (item) => item.toString(),
value: (item) => item/// item);
map['1'] + map['2']; // 1 + 4
map['3'] - map['2']; // 9 - 4
If no values are specified for key
and value
the default is the
identity function.
In the following example, the keys and corresponding values of map
are list
values:
map = Map.fromIterable(list);
map[1] + map[2]; // 1 + 2
map[3] - map[2]; // 3 - 2
The keys computed by the source iterable
do not need to be unique. The
last occurrence of a key will simply overwrite any previous value.
Implementation
factory StreamMap.fromIterable(
Iterable iterable, {
_Compute<K>? key,
_Compute<V>? value,
OnUpdate<Map<K, V>>? onUpdate,
OnEvent<CollectionEvent<K, V>>? onEvent,
OnChange<CollectionChangeEvent<K, V>>? onChange,
}) {
return StreamMap<K, V>(
value: Map.fromIterable(iterable, key: key, value: value),
onUpdate: onUpdate,
onEvent: onEvent,
onChange: onChange,
);
}