when<R> method
R?
when<R>({
- WhenValueReturn<
T, R> ? standby, - WhenValueReturn<
T, R> ? loading, - WhenValueReturn<
T, R> ? done, - WhenErrorReturn<
R> ? error,
Returns a new value of R
depending on the state of the hook:
standby
: When the state has the initial value.
loading
: When the request for the state is retrieving the value.
done
: When the request is done.
error
: If any errors happens in the request.
For example:
final valueComputed = appContext.asyncState.when<String>(
standby: (value) => "⚓️ Standby: ${value}",
loading: (value) => "⏳ Loading...",
done: (value) => "✅ Resolved: ${value}",
error: (error) => "❌ Error: ${error}",
)
Implementation
R? when<R>({
WhenValueReturn<T, R>? standby,
WhenValueReturn<T, R>? loading,
WhenValueReturn<T, R>? done,
WhenErrorReturn<R>? error,
}) {
if (status == UseAsyncStateStatus.error) {
return error?.call(this.error);
}
if (status == UseAsyncStateStatus.loading) {
return loading?.call(value);
}
if (status == UseAsyncStateStatus.done) {
return done?.call(value);
}
return standby?.call(value);
}