castMapKeys method
Cast map
keys to reflectedType if type
== reflectedType or return null
.
- If
nullable
istrue
casts to a Map of nullable keys.
Implementation
Map? castMapKeys(Map map, TypeInfo typeInfo, {bool nullable = false}) {
if (!typeInfo.isMap) {
return map;
}
var keyType = typeInfo.argumentType(0) ?? TypeInfo.tDynamic;
var valueType = typeInfo.argumentType(1) ?? TypeInfo.tDynamic;
if (keyType.type == reflectedType) {
Map? callVal<V>() => map is Map<O, V>
? map
: map.map<O, V>((key, value) => MapEntry<O, V>(key, value));
Map? callValNullable<V>() => map is Map<O?, V>
? map
: map.map<O?, V>((key, value) => MapEntry<O?, V>(key, value));
var m = nullable
? valueType.callCasted(callValNullable)
: valueType.callCasted(callVal);
return m;
} else {
Map? callKey<K>() => valueType.callCasted(<V>() {
return map is Map<K, V>
? map
: map.map<K, V>((key, value) => MapEntry<K, V>(key, value));
});
Map? callKeyNullable<K>() => valueType.callCasted(<V>() {
return map is Map<K, V?>
? map
: map.map<K, V?>((key, value) => MapEntry<K, V?>(key, value));
});
var m = nullable
? keyType.callCasted(callKeyNullable)
: keyType.callCasted(callKey);
return m;
}
}