splitNamed<R> method
R?
splitNamed<R>({})
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);
}