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 normalizeKeyLookup = false,
  7. T onError(
    1. 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);
  }
}