map<Result> method

Result? map<Result>({
  1. required Result ready(
    1. RepositoryStateReady<Data> state
    ),
  2. Result? empty(
    1. RepositoryStateEmpty<Data> state
    )?,
})

Returns the value of the current state of the repository. It can be either RepositoryStateEmpty or RepositoryStateReady. It throws an Exception if the state is not handled.

The map method is useful when you want to handle the state of the repository. For example you can map empty state to a loading indicator and ready state to a list of items.

Implementation

Result? map<Result>({
  required Result Function(RepositoryStateReady<Data> state) ready,
  Result? Function(RepositoryStateEmpty<Data> state)? empty,
}) {
  final self = this;

  return switch (self) {
    RepositoryStateEmpty<Data> _ => empty?.call(self),
    RepositoryStateReady<Data> _ => ready.call(self),
  };
}