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

Inheritance

Constructors

UseAsyncState.new(AsyncFunction<T> asyncFunction, T initialValue, {String? debugLabel})
A ReactteHook that manages the state as async way.

Properties

$ → HookBindingZone<IHook>
This variable is used to register IHook and attach the IState that are defined here.
finalinherited
boundInstance Object?
The reference instance to the current state.
no setterinherited
debugInfo Map<String, dynamic>
A map containing information about state for debugging purposes.
no setterinherited
debugLabel String?
A label used for debugging purposes.
no setterinherited
dependencyInjection → DependencyInjection
no setterinherited
error Object?
no setterinherited
eventHandler → EventHandler
no setterinherited
future Future<T>
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
isDisposed bool
Returns true if the state has been disposed.
no setterinherited
isDone bool
no setterinherited
isError bool
no setterinherited
isLoading bool
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
stateManagement → StateManagement<IState>
no setterinherited
status UseAsyncStateStatus
no setterinherited
uError UseCompute<Object?>
latefinalinherited
uIsDone UseCompute<bool>
latefinalinherited
uIsError UseCompute<bool>
latefinalinherited
uIsLoading UseCompute<bool>
latefinalinherited
uStatus UseCompute<UseAsyncStateStatus>
latefinalinherited
uValue UseCompute<T>
latefinalinherited
value → T
no setterinherited

Methods

bind(Object instance) → void
Stores a reference to an object instance
inherited
cancel() → void
Cancels the async function execution.
inherited
dispose() → void
Called when this object is removed
inherited
initHook() → void
Initializes the hook. This method is called when the hook is created. You can override this method to ensure that the hook is properly initialized and to bind the instance to the states. This is particularly useful for setting up state dependencies or performing side effects when the hook is first created.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notify() → 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 dynamic fnUpdate()?]) → void
Executes fnUpdate, and notify the listeners about to update.
inherited
when<R>({WhenValueReturn<T, R>? idle, 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>(AsyncFunctionArg<T, A> asyncFunction, T initialValue, {String? debugLabel}) UseAsyncStateArg<T, A>
A ReactteHook that manages the state as async way.