maybeWhen<R> method
R
maybeWhen<R>({
- R loading()?,
- R success(
- T data
- R error(
- Object error
- R empty()?,
- R custom()?,
- 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();
}