guard method

Future<void> guard(
  1. Future<T> operation(), {
  2. bool preserveData = true,
})

Executes operation, setting state to loading, then success or error. Preserves previous data during refresh when preserveData is true.

Implementation

Future<void> guard(
  Future<T> Function() operation, {
  bool preserveData = true,
}) async {
  _preserveData = preserveData;
  if (preserveData) {
    state = AsyncLoading<T>().copyWithPrevious(state);
  } else {
    state = const AsyncLoading();
  }
  state = await AsyncValue.guard(operation);
}