castMap method
Cast map
keys & values to reflectedType if type
== reflectedType or return null
.
- If
nullable
istrue
casts to a Map of nullable key & values. - See castMapKeys and castMapValues.
Implementation
Map? castMap(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 && valueType.type == reflectedType) {
var m = nullable
? map.map<O?, O?>((key, value) => MapEntry<O?, O?>(key, value))
: map.map<O, O>((key, value) => MapEntry<O, O>(key, value));
return m;
} else 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 if (valueType.type == reflectedType) {
Map? callKey<K>() => map is Map<K, O>
? map
: map.map<K, O>((key, value) => MapEntry<K, O>(key, value));
Map? callKeyNullable<K>() => map is Map<K, O?>
? map
: map.map<K, O?>((key, value) => MapEntry<K, O?>(key, value));
var m = nullable
? keyType.callCasted(callKeyNullable)
: keyType.callCasted(callKey);
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;
}
}