UseAsyncState<T, A> class
A ReactteHook that manages the state as async way.
UseAsyncState has two types T and A(UseAsyncState<T, A>).
T is use to define the type of value
and A is use to define the type of resolve argument.
These types can be deferred depending on the initial value and resolve method defined.
This example produces one simple UseAsyncState:
class AppController {
// It's same that: late final asyncState = UseAsyncState<String, String?>(
final asyncState = UseAsyncState("Initial value", _resolveState);
Future<String> _resolveState([String? value = "Default value"]) async {
return Future.delayed(
const Duration(seconds: 1),
() => value,
);
}
}
Use resolve method to resolve state and use 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 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 initial state using the reset method.
- Inheritance
-
- Object
- ReactterHook
- UseAsyncState
Constructors
-
UseAsyncState(T initial, AsyncFunction<
T, A> asyncValue)
Properties
- $ → _HookRegister
-
This variable is used to register
ReacttrHookand attach the ReactterState that are defined here.final -
asyncValue
→ AsyncFunction<
T, A> -
Works as a the value initializer.
Need to call resolve to execute.
final
- error → Object?
-
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- instanceAttached → Object?
-
no setterinherited
- isDisposed → bool
-
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- status → UseAsyncStateStatus
-
no setter
- value → T
-
no setter
Methods
-
attachTo(
Object instance) → void -
Attaches an object instance to this state.
inherited
-
createState(
) → void -
Adds the current state to a list if instances of the Reactter class are being built.
inherited
-
detachInstance(
) → void -
Detaches an object instance to this state.
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 -
Forces update and notifies to listeners that it did update
inherited
-
reset(
) → void -
Reset value, status and error to its
initialstate. -
resolve(
[A? arg]) → Future< T?> - Execute asyncValue to resolve value.
-
toString(
) → String -
A string representation of this object.
inherited
-
update(
[covariant Function? callback]) → void -
Executes
callback, and notify the listeners about to update.inherited -
updateAsync(
[covariant Function? callback]) → Future< void> -
Executes
callback, and notify the listeners about to update as async way.inherited -
when<
R> ({WhenValueReturn< T, R> ? standby, WhenValueReturn<T, R> ? loading, WhenValueReturn<T, R> ? done, WhenErrorReturn<R> ? error}) → R? -
Returns a new value of
Rdepending on the state of the hook:
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited