UseAsyncStateArg<T, A> class
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.
- Inheritance
-
- Object
- ReactterHook
- UseAsyncStateArg
Constructors
-
UseAsyncStateArg(T initialValue, AsyncFunctionArg<
T, A> asyncFunction) -
A
ReactteHook
that manages the state as async way.
Properties
- $ → HookRegister
-
This variable is used to register
Hook
and attach theStateBase
that are defined here.finalinherited - error → Object?
-
no setterinherited
- eventHandler → EventHandler
-
no setterinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- instanceBinded → Object?
-
The reference instance to the current state.
no setterinherited
- instanceInjection → DependencyInjection
-
no setterinherited
- isDisposed → bool
-
Returns
true
if the state has been disposed.no setterinherited - logger → Logger
-
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
stateManagment
→ StateManagement<
StateBase> -
no setterinherited
- status → UseAsyncStateStatus
-
no setterinherited
- value → T
-
no setterinherited
Methods
-
bind(
Object instance) → void -
Stores a reference to an object instance
inherited
-
dispose(
) → void -
Called when this object is removed
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
refresh(
) → void -
It's used to notify listeners that the state has been updated.
It is typically called after making changes to the state object.
inherited
-
reset(
) → void -
Reset value, status and error to its
initial
state.inherited -
resolve(
A arg) → FutureOr< T?> -
Execute
asyncFunction
to resolve value. -
toString(
) → String -
A string representation of this object.
inherited
-
unbind(
) → void -
Removes the reference to the object instance
inherited
-
update(
[covariant Function? callback]) → void -
Executes
callback
, and notifies the listeners about the update.inherited -
when<
R> ({WhenValueReturn< T, R> ? standby, WhenValueReturn<T, R> ? loading, WhenValueReturn<T, R> ? done, WhenErrorReturn<R> ? error}) → R? -
Returns a new value of
R
depending on the state of the hook:inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited