get<T> method
Returns the value of the given key
in the data map.
This tries to cast or parse the value to the given T
type. If it fails, then value from defaultValue
is returned if defaultValue
callback is provided.
Implementation
T? get<T>(
String key, [
ReturnDefaultValueCallback<T>? defaultValue,
]) {
final val = data[key];
if (val == null) return null;
if (val is T) return val;
if (T == String) {
return val.toString() as T?;
} else if (T == num) {
if (val is num) return val as T?;
return num.tryParse(val.toString()) as T?;
} else if (T == int) {
if (val is int) return val as T?;
return int.tryParse(val.toString()) as T?;
} else if (T == double) {
if (val is double) return val as T?;
return double.tryParse(val.toString()) as T?;
}
return defaultValue?.call();
}