HashMap<K, V>.from构造函数

HashMap<K, V>.from(
  1. Map other
)

创建一个包含other中所有的键/值对的新HashMap

键必须都是K的实例,且值是V。而other映射可以是任何类型。

final baseMap = {1: 'A', 2: 'B', 3: 'C'};
final fromBaseMap = HashMap<int, String>.from(baseMap);
print(fromBaseMap); // {1: A, 2: B, 3: C}

实现

factory HashMap.from(Map<dynamic, dynamic> other) {
  HashMap<K, V> result = HashMap<K, V>();
  other.forEach((dynamic k, dynamic v) {
    result[k as K] = v as V;
  });
  return result;
}