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 AppContext extends ReactterContext {
  // It's same that: late final asyncState = UseAsyncState<String, String?>(
  late final asyncState = UseAsyncState(
    "Initial value",
    _resolveState,
    this,
  );

  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: "${appContext.asyncState.value}"');
  // Resolve state
  await appContext.asyncState.resolve("Resolved value");
  // After changed value: "Resolved value"
  print('After changed value: "${appContext.asyncState.value}"');

It also has when method that returns a new value depending on it's state:

final valueComputed = appContext.asyncState.when<String>(
  standby: (value) => "⚓️ Standby: $value",
  loading: (value) => "⏳ Loading...",
  done: (value) => "✅ Resolved: $value",
  error: (error) => "❌ Error: $error",
);

You can use the getters _state and error for more detail and you could reset the state using reset method.

Inheritance

Constructors

UseAsyncState(dynamic initial, AsyncFunction<T, A> asyncValue, [ReactterHookManager? context])

Properties

asyncValue AsyncFunction<T, A>
Works as a the value initializer. Need to call resolve to execute.
final
context ReactterHookManager?
getter/setter pair
error Object?
no setter
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
status UseAsyncStateStatus
no setter
value → T
no setter

Methods

listenHooks(List<ReactterHook> hooks) → void
Suscribes to all hooks given.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
reset() → void
Reset value, status and error to its initial state.
resolve([A? arg]) Future<T?>
Execute asyncValue to resolve value.
toString() String
A string representation of this object.
inherited
update([Function? callback]) → void
First, invokes the subscribers callbacks of the willUpdate event.
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:

Operators

operator ==(Object other) bool
The equality operator.
inherited