SortedMap<K extends Comparable, V>.fromIterable constructor Null safety

SortedMap<K extends Comparable, V>.fromIterable(
  1. Iterable iterable,
  2. {K key(
    1. dynamic
    )?,
  3. V value(
    1. dynamic
    )?,
  4. Ordering ordering = const Ordering.byKey()}
)

Creates a SortedMap where the keys and values are computed from the iterable.

For each element of the iterable this constructor computes a key/value pair, by applying key and value respectively.

The keys of the key/value pairs do not need to be unique. The last occurrence of a key will simply overwrite any previous value.

If no functions are specified for key and value the default is to use the iterable value itself.

Implementation

factory SortedMap.fromIterable(Iterable iterable,
    {K Function(dynamic)? key,
    V Function(dynamic)? value,
    Ordering ordering = const Ordering.byKey()}) {
  var map = SortedMap<K, V>(ordering);

  key ??= (v) => v;
  value ??= (v) => v;
  for (var element in iterable) {
    map[key(element)] = value(element);
  }
  return map;
}