execute method

Future<void> execute(
  1. Future<T> operation()
)

Executes an asynchronous operation and updates the state accordingly.

On success: updates data and sets state to AsyncState.success. On error: sets error message and updates state to AsyncState.error.

Implementation

Future<void> execute(Future<T> Function() operation) async {
  try {
    _state.value = AsyncState.loading;
    _error.value = null;

    final result = await operation();

    _data.value = result;
    _state.value = AsyncState.success;
  } catch (e) {
    _error.value = e.toString();
    _state.value = AsyncState.error;
  }
}