UseAsyncState<T> constructor

UseAsyncState<T>(
  1. T initialValue,
  2. AsyncFunction<T> asyncFunction
)

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:

Implementation

UseAsyncState(
  T initialValue,
  AsyncFunction<T> asyncFunction,
) : super(initialValue, asyncFunction);