run method

Future<void> run(
  1. Future<T> task()
)

Runs the asynchronous task and updates the state accordingly.

  • Sets loading state before execution.
  • If successful, sets the data.
  • If error occurs, captures and stores the error.

Implementation

Future<void> run(Future<T> Function() task) async {
  state.value = CoderAsyncState<T>.loading();
  try {
    final result = await task();
    state.value = CoderAsyncState<T>.data(result);
  } catch (e) {
    state.value = CoderAsyncState<T>.error(e.toString());
  }
}