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

$ → _ReactterHookRegister
This variable is used to register ReactterHook and attach the ReactterState that are defined here.
finalinherited
error Object?
no setterinherited
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 setterinherited
value → T
no setterinherited

Methods

attachTo(Object instance) → void
Attaches an object instance to this state.
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
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
update([covariant Function? callback]) → void
Executes callback, and notify the listeners about to 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.