UseAsyncState<T> constructor
UseAsyncState<T> (
- AsyncFunction<
T> asyncFunction, - T initialValue, {
- String? debugLabel,
A ReactteHook
that manages the state as async way.
T
is use to define the type of value
.
This example produces one simple UseAsyncState:
class AppController {
// It's same that: UseAsyncState<String>
final asyncState = UseAsyncState(
"Initial value",
() => Future.delayed(
const Duration(seconds: 1),
() => "Resolved value"
);
}
}
Use the resolve method to resolve state
and use the value
getter to get state:
// Before changed value: "Initial value"
print('Before changed value: "${appController.asyncState.value}"');
// Resolve state
await appController.asyncState.resolve();
// After changed value: "Resolved value"
print('After changed value: "${appController.asyncState.value}"');
It also has the when
method that returns a new value
depending on it's state:
final valueComputed = appController.asyncState.when<String>(
idle: (value) => "⚓️ Standby: $value",
loading: (value) => "⏳ Loading...",
done: (value) => "✅ Resolved: $value",
error: (error) => "❌ Error: $error",
);
Its status may be obtained using the getters value
and error
,
and restore it to its initial
state using the reset
method.
See also:
- UseAsyncStateArg, the same as it, but with arguments.
Implementation
UseAsyncState(
AsyncFunction<T> asyncFunction,
T initialValue, {
String? debugLabel,
}) : super(
asyncFunction,
initialValue,
debugLabel: debugLabel,
);