valueAs<T extends Object?> method
T
valueAs<T extends Object?>(
- K key, {
- bool? allowHex,
- StringEncoding? encoding,
- bool asBytes = false,
- T onMissing()?,
- bool acceptSnakeCase = false,
- bool acceptCamelCase = false,
- T onError(
- JsonParserError err
Implementation
T valueAs<T extends Object?>(
K key, {
bool? allowHex,
StringEncoding? encoding,
bool asBytes = false,
T Function()? onMissing,
/// Also accepts the snake_case or camel_case version of [key] when looking up JSON fields.
/// Only applicable when [K] is `String`.
bool acceptSnakeCase = false,
bool acceptCamelCase = false,
T Function(JsonParserError err)? onError,
}) {
assert(
(allowHex == null && encoding == null) || asBytes,
"allowHex and encoding must be use with asBytes",
);
try {
final value = _checkItem<T>(
key,
onMissing,
acceptSnakeCase,
acceptCamelCase,
);
if (value == null) return null as T;
return JsonParser.valueAs(
value,
allowHex: allowHex,
asBytes: asBytes,
encoding: encoding,
);
} on JsonParserError catch (e) {
if (onError != null) return onError(e);
rethrow;
}
}