splitNamed<R> method

R? splitNamed<R>({
  1. R onNull()?,
  2. R onBool(
    1. bool
    )?,
  3. R onInt(
    1. int
    )?,
  4. R onDouble(
    1. double
    )?,
  5. R onString(
    1. String
    )?,
  6. R onList(
    1. List<Json>
    )?,
  7. R onMap(
    1. Map<String, Json>
    )?,
  8. R onOther(
    1. Object?
    )?,
  9. R onInvalid(
    1. Object?
    )?,
})

Handle selected possible shapes of this JSON value.

This method is used to discriminate some or all of the possible shapes of this JSON value, similarly to a switch expression. It accepts a function per shape, using an optional named parameter. If the actual case is not handled (say, it's a String, and no actual argument named onString was provided, or it was null) then onOther is invoked. Finally, onInvalid is invoked in the case where this is not a valid JSON value.

Implementation

R? splitNamed<R>({
  R Function()? onNull,
  R Function(bool)? onBool,
  R Function(int)? onInt,
  R Function(double)? onDouble,
  R Function(String)? onString,
  R Function(List<Json>)? onList,
  R Function(Map<String, Json>)? onMap,
  R Function(Object?)? onOther,
  R Function(Object?)? onInvalid,
}) {
  var v = value;
  if (v == null) return onNull != null ? onNull() : onOther?.call(null);
  if (v is bool) return (onBool ?? onOther)?.call(v);
  if (v is int) return (onInt ?? onOther)?.call(v);
  if (v is double) return (onDouble ?? onOther)?.call(v);
  if (v is String) return (onString ?? onOther)?.call(v);
  if (v is List<Json>) return (onList ?? onOther)?.call(v);
  if (v is Map<String, Json>) return (onMap ?? onOther)?.call(v);
  if (onInvalid != null) return onInvalid(v);
  throw InvalidJsonTypeException(value);
}