when<R> method

R when<R>({
  1. required R loading(),
  2. required R data(
    1. T data
    ),
  3. required R failure(
    1. AppError error
    ),
})

Pattern matching

Implementation

R when<R>({
  required R Function() loading,
  required R Function(T data) data,
  required R Function(AppError error) failure,
}) {
  if (this is AppLoading<T>) return loading();
  if (this is AppData<T>) return data((this as AppData<T>).data);
  if (this is AppFailure<T>) return failure((this as AppFailure<T>).error);
  throw Exception('Unhandled state');
}