map<NewT> method

NewT map<NewT>({
  1. required NewT data(
    1. AsyncData<ValueT> data
    ),
  2. required NewT error(
    1. AsyncError<ValueT> error
    ),
  3. required NewT loading(
    1. AsyncLoading<ValueT> loading
    ),
})

Perform some action based on the current state of the AsyncValue.

This allows reading the content of an AsyncValue in a type-safe way, without potentially ignoring to handle a case.

Implementation

NewT map<NewT>({
  required NewT Function(AsyncData<ValueT> data) data,
  required NewT Function(AsyncError<ValueT> error) error,
  required NewT Function(AsyncLoading<ValueT> loading) loading,
}) {
  final that = this;
  switch (that) {
    case AsyncLoading():
      return loading(that);
    case AsyncData():
      return data(that);
    case AsyncError():
      return error(that);
  }
}