getValue<T> method
T
getValue<T>(
- CborObject key
Retrieves the value associated with the specified key
from the CborMapValue
.
If key
does not exist in the map and T
is nullable, returns null. Otherwise, throws a MessageException
.
Implementation
T getValue<T>(CborObject key) {
if (!value.containsKey(key)) {
if (null is T) return null as T;
throw MessageException("Key does not exist.", details: {
"Key": "${key.runtimeType}",
"Keys": value.keys.map((e) => e.runtimeType).join(", ")
});
}
final val = value[key];
if (null is T && val is CborNullValue) return null as T;
if (val is CborObject && val.value is T) return val.value;
if (val is! T) {
throw MessageException("Failed to cast value.",
details: {"Excepted": "${val.runtimeType}", "Type": "$T"});
}
return val;
}