UseAsyncState<T> class

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>(
  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.

See also:

Inheritance

Constructors

UseAsyncState(T initialValue, AsyncFunction<T> asyncFunction)
A ReactteHook that manages the state as async way.

Properties

$ → HookRegister
This variable is used to register Hook and attach the StateBase that are defined here.
finalinherited
error Object?
no setterinherited
eventManager → EventManager
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
instanceBinded Object?
The reference instance to the current state.
no setterinherited
instanceManager → InstanceManager
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
stateManager → StateManager<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() 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

Static Methods

withArg<T, A>(T initialValue, AsyncFunctionArg<T, A> asyncFunction) UseAsyncStateArg<T, A>
A ReactteHook that manages the state as async way.