toNullableValueMapOf<K extends Object, V extends Object> function

Map<K, V?> toNullableValueMapOf<K extends Object, V extends Object>(
  1. Map<K, Object?> map, {
  2. bool isExposed = false,
})

Converts map to a value map of K to V?, or throws if cannot convert.

The returned map may contain null values.

Supported types for V: String, num, int, BigInt, double, bool, DateTime, Identifier, Object.

Implementation

Map<K, V?> toNullableValueMapOf<K extends Object, V extends Object>(
  Map<K, Object?> map, {
  bool isExposed = false,
}) {
  if (map is Map<K, V?>) {
    return isExposed ? Map<K, V?>.unmodifiable(map) : map;
  } else if (map is Map<K, V>) {
    return isExposed ? Map<K, V?>.unmodifiable(map) : map.cast<K, V?>();
  } else {
    return map.map<K, V?>(
      (key, value) => MapEntry<K, V?>(key, toNullableValueOf<V>(value)),
    );
  }
}