when<T> method

T when<T>({
  1. required T map(
    1. Map<String, Json>
    ),
  2. required T list(
    1. List<Json>
    ),
  3. required T number(
    1. num
    ),
  4. required T boolean(
    1. bool
    ),
  5. required T str(
    1. String
    ),
  6. required T none(
    1. Null
    ),
})

Executes the given callback when [this.value is of the type parameter.

Implementation

T when<T>({
  required T Function(Map<String, Json>) map,
  required T Function(List<Json>) list,
  required T Function(num) number,
  required T Function(bool) boolean,
  required T Function(String) str,
  required T Function(Null) none,
}) {
  final v = this;
  if (v is JsonMap) {
    return map(v.value);
  } else if (v is JsonList) {
    return list(v.value);
  } else if (v is JsonNumber) {
    return number(v.value);
  } else if (v is JsonBoolean) {
    return boolean(v.value);
  } else if (v is JsonStr) {
    return str(v.value);
  } else if (v is JsonNone) {
    return none(null);
  }
  throw Error();
}