deepCopyMap<K, V> function

Map<K, V>? deepCopyMap<K, V>(
  1. Map<K, V>? map
)

Deeply copies map.

Implementation

Map<K, V>? deepCopyMap<K, V>(Map<K, V>? map) {
  if (map == null) return null;

  if (map is Map<String, String>) {
    return Map<String, String>.from(map) as Map<K, V>;
  } else if (map is Map<String, int>) {
    return Map<String, int>.from(map) as Map<K, V>;
  } else if (map is Map<String, double>) {
    return Map<String, double>.from(map) as Map<K, V>;
  } else if (map is Map<String, num>) {
    return Map<String, num>.from(map) as Map<K, V>;
  } else if (map is Map<String, bool>) {
    return Map<String, bool>.from(map) as Map<K, V>;
  }

  if (K == dynamic || K == Object) {
    if (map is Map<String, Object?>) {
      return map.map<String, V>(
              (k, V v) => MapEntry<String, V>(k as String, deepCopy(v) as V))
          as Map<K, V>;
    }
  }

  if (map.isEmpty) return <K, V>{};

  return map.map<K, V>(
      (K k, V v) => MapEntry<K, V>(deepCopy(k) as K, deepCopy(v) as V));
}