Manages all the different states of a [Resource]:
Returns true if the Resource is in the loading state.
Returns true if the Resource is in the ready state.
Returns true if the Resource is in the error state.
Returns true if the Resource is being refreshed.
Upcast ResourceState into a ResourceReady, or return null if the ResourceState is in loading/error state.
Upcast ResourceState into a ResourceError, or return null if the ResourceState is in read/loading state.
Attempts to synchronously get the value of ResourceReady.
On error, this will rethrow the error.
If loading, will return null.
Attempts to synchronously get the value of ResourceReady.
On error, this will rethrow the error.
If loading, will return null.
Attempts to synchronously get the error of ResourceError.
On other states will return null.
R maybeMap<R>({required R Function() orElse, R Function(ResourceReady<T> ready)? ready, R Function(ResourceError<T> error)? error, R Function(ResourceLoading<T> loading)? loading})
Perform some actions based on the state of the ResourceState, or call orElse if the current state is not considered.
resourceState.maybeMap(
ready: (resourceReady) => Text(resourceReady.value),
orElse: () => const SizedBox(),
);The function above renders the Text widget only in the ready state, for all other state the orElse function is called.
Prefer using the maybeOn method if you are not interested in the ResourceState object.
R on<R>({required R Function(T data) ready, required R Function(Object error, StackTrace? stackTrace) error, required R Function() loading})
Performs an action based on the state of the ResourceState.
All cases are required.
resourceState.on(
ready: (data) => Text('ready: $data'),
error: (error, stackTrace) => Text('error: $error $stackTrace'),
loading: () => Text('loading'),
);R maybeOn<R>({required R Function() orElse, R Function(T data)? ready, R Function(Object error, StackTrace? stackTrace)? error, R Function()? loading})
Performs an action based on the state of the ResourceState, or call orElse if the current state is not considered.
resourceState.maybeOn(
ready: (data) => Text(data),
orElse: () => const SizedBox(),
);The function above renders the Text widget only in the ready state, for all other state the orElse function is called.
The ResourceUnresolved state indicates that the Resource is not resolved yet, this state is internal and should never be visible outside.
The ResourceReady state indicates that the Resource is resolved and ready to use.
ResourceReady(T value, {bool isRefreshing = false});value is the resolved value of the Resource.
isRefreshing is true if the Resource is being refreshed.
Returns the string representation of the ResourceReady state.
Convenience method to copy the current state and create a new ResourceReady state with the overridden values.
The ResourceLoading state indicates that the Resource is loading.
Returns the string representation of the ResourceLoading state.
The ResourceError state indicates that the Resource failed to load and is in an error state.
ResourceError(
Object error, {
StackTrace? stackTrace,
bool isRefreshing = false,
});error is the error that occurred.
stackTrace is the stack trace of the error, if present.
isRefreshing is true if the Resource is being refreshed.
Returns the string representation of the ResourceError state.
Convenience method to copy the current state and create a new ResourceError state with the overridden values.
ResourceState is a sealed class, so you can perform a switch on the different states instead of using on, maybeOn, etc.
return switch (userState) {
ResourceReady(:final value) => Text(value),
ResourceError(:final error, :final stackTrace) =>
Text('$error, $stackTrace'),
ResourceLoading() => const CircularProgressIndicator(),
}