StreamMap<K, V>.fromIterable constructor

StreamMap<K, V>.fromIterable(
  1. Iterable iterable, {
  2. _Compute<K>? key,
  3. _Compute<V>? value,
  4. OnUpdate<Map<K, V>>? onUpdate,
  5. OnEvent<CollectionEvent<K, V>>? onEvent,
  6. OnChange<CollectionChangeEvent<K, V>>? onChange,
})

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,
  );
}