whenOrElse<W> method

W whenOrElse<W>({
  1. W loading()?,
  2. W loaded(
    1. D data
    )?,
  3. W error(
    1. dynamic error,
    2. String message
    )?,
  4. required W orElse(),
})

Pattern matching for state types, with a required fallback.

Returns the result of the matching callback, or orElse if no match is found.

Implementation

W whenOrElse<W>({
  W Function()? loading,
  W Function(D data)? loaded,
  W Function(dynamic error, String message)? error,
  required W Function() orElse,
}) {
  if (this is NotifierStateLoading) {
    return loading?.call() ?? orElse();
  } else if (this is NotifierStateLoaded) {
    return loaded?.call((this as NotifierStateLoaded<T, D>).data) ?? orElse();
  } else if (this is NotifierStateError) {
    return error?.call(
          (this as NotifierStateError).error,
          (this as NotifierStateError).message,
        ) ??
        orElse();
  }
  return orElse();
}