maybeWhen<R> method

R maybeWhen<R>({
  1. R loading()?,
  2. R success(
    1. T data
    )?,
  3. R error(
    1. Object error
    )?,
  4. R empty()?,
  5. R custom()?,
  6. required R orElse(),
})

Partial pattern matching with a required orElse fallback.

status.maybeWhen(
  success: (data) => showContent(data),
  orElse: () => showSpinner(),
);

Implementation

R maybeWhen<R>({
  R Function()? loading,
  R Function(T data)? success,
  R Function(Object error)? error,
  R Function()? empty,
  R Function()? custom,
  required R Function() orElse,
}) {
  if (this is SuccessStatus<T> && success != null) {
    return success((this as SuccessStatus<T>).data);
  } else if (this is ErrorStatus<T, Object> && error != null) {
    return error((this as ErrorStatus<T, Object>).error ?? 'Unknown error');
  } else if (this is LoadingStatus<T> && loading != null) {
    return loading();
  } else if (this is EmptyStatus<T> && empty != null) {
    return empty();
  } else if (this is CustomStatus<T> && custom != null) {
    return custom();
  }
  return orElse();
}