when<R> method
R
when<R>({
- required R loading(),
- required R success(
- T data
- required R error(
- Object error
- required R empty(),
- R custom()?,
Exhaustive pattern matching — all cases must be handled.
status.when(
loading: () => showSpinner(),
success: (data) => showContent(data),
error: (err) => showError(err),
empty: () => showEmpty(),
);
Implementation
R when<R>({
required R Function() loading,
required R Function(T data) success,
required R Function(Object error) error,
required R Function() empty,
R Function()? custom,
}) {
if (this is SuccessStatus<T>) {
return success((this as SuccessStatus<T>).data);
} else if (this is ErrorStatus<T, Object>) {
return error((this as ErrorStatus<T, Object>).error ?? 'Unknown error');
} else if (this is LoadingStatus<T>) {
return loading();
} else if (this is EmptyStatus<T>) {
return empty();
} else {
return (custom ?? empty)();
}
}