valueAs<T extends Object?> method
T
valueAs<T extends Object?>(
- K key, {
- bool? allowHex,
- StringEncoding? encoding,
- bool asBytes = false,
- T onMissing()?,
- bool normalizeKeyLookup = false,
- T onError(
- BuError err
Reads key and coerces it to whichever concrete type T is.
Implementation
T valueAs<T extends Object?>(
K key, {
bool? allowHex,
StringEncoding? encoding,
bool asBytes = false,
T Function()? onMissing,
/// Only applicable when [K] is `String`.
bool normalizeKeyLookup = false,
T Function(BuError err)? onError,
}) {
assert(
(allowHex == null && encoding == null) || asBytes,
"allowHex and encoding must be use with asBytes",
);
try {
final value = _checkItem<T>(key, onMissing, normalizeKeyLookup);
if (value == null) return null as T;
return JsonParser.valueAs(
value,
allowHex: allowHex,
asBytes: asBytes,
encoding: encoding,
);
} on BuError catch (e) {
if (onError != null) return onError(e);
throw CoreErrors.jsonErrorWithKey(e, key);
}
}