valueAs<T extends Object?> method

T valueAs<T extends Object?>(
  1. K key, {
  2. bool? allowHex,
  3. StringEncoding? encoding,
  4. bool asBytes = false,
  5. T onMissing()?,
  6. bool acceptSnakeCase = false,
  7. bool acceptCamelCase = false,
  8. T onError(
    1. 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;
  }
}