withArg<T, A> static method

UseAsyncStateArg<T, A> withArg<T, A>(
  1. T initialValue,
  2. AsyncFunctionArg<T, A> asyncFunction
)

A ReactteHook that manages the state as async way.

T is use to define the type of value and A is use to define the type of resolve argument.

This example produces one simple UseAsyncStateArg:

class AppController {
  // It's same that: UseAsyncStateArg<String, Args1<String>>
  final asyncState = UseAsyncStateArg(
    "Initial value",
    (Args1<String> args) => Future.delayed(
      const Duration(seconds: 1),
      () => args.arg,
    ),
  });

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("Resolved value");
  // 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 initialValue state using the reset method.

See also:

Implementation

static UseAsyncStateArg<T, A> withArg<T, A>(
  T initialValue,
  AsyncFunctionArg<T, A> asyncFunction,
) {
  return UseAsyncStateArg<T, A>(initialValue, asyncFunction);
}