withArg<T, A> static method
UseAsyncStateArg<T, A>
withArg<T, A>(
- AsyncFunctionArg<
T, A> asyncFunction, - T initialValue, {
- String? debugLabel,
A ReactteHook
that manages the state as async way.
T
is use to define the type of value
and A
is use to define the type of resolve argument.
This example produces one simple UseAsyncStateArg:
class AppController {
// It's same that: UseAsyncStateArg<String, Args1<String>>
final asyncState = UseAsyncStateArg(
"Initial value",
(Args1<String> args) => Future.delayed(
const Duration(seconds: 1),
() => args.arg,
),
});
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("Resolved value");
// 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>(
standby: (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 initialValue
state using the reset
method.
See also:
- UseAsyncState, the same as it, but without arguments.
- Args, a generic arguments.
Implementation
static UseAsyncStateArg<T, A> withArg<T, A>(
AsyncFunctionArg<T, A> asyncFunction,
T initialValue, {
String? debugLabel,
}) {
return UseAsyncStateArg<T, A>(
asyncFunction,
initialValue,
debugLabel: debugLabel,
);
}