when<T> method

  1. @override
T when<T>({
  1. required T loading(),
  2. required T data(
    1. S data
    ),
  3. required T error(
    1. ErrorState e
    ),
})
override

Pattern matching for async state.

loading is called if the state is loading. data is called if the state contains data. error is called if the state contains an error.

Returns the result of the matching callback.

Implementation

@override
T when<T>({
  required T Function() loading,
  required T Function(S data) data,
  required T Function(ErrorState e) error,
}) {
  switch (_status) {
    case AsyncStatus.loading:
      return loading();
    case AsyncStatus.error:
      return error(errorState!);
    case AsyncStatus.data:
      return data(this.data as S);
  }
}