toMap<K, V> method

Map<K, V> toMap<K, V>(
  1. MapEntry<K, V>? f(
    1. T item
    )
)

The callback given by f from Iterable will create a map of K and V.

The return value of f must be MapEntry.

If the return value of f is Null, it is removed from the element.

If there are duplicate keys, the earlier element takes precedence.

Iterableからfで与えられたコールバックによってKVのマップを作成します。

fの戻り値はMapEntryである必要があります。

fの戻り値をNullにした場合は要素から削除されます。

キーが重複した場合、先の要素が優先されます。

final array = [1, 2, 3, 4, 5, 6];
final map = array.toMap<String, int>(
  (item) => MapEntry(item.toString(), item);
); // {"1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6 }

Implementation

Map<K, V> toMap<K, V>(
  MapEntry<K, V>? Function(T item) f,
) {
  return Map.fromEntries(map((e) => f.call(e)).removeEmpty());
}