guard<T> static method

Future<AsyncValue<T>> guard<T>(
  1. Future<T> future()
)

Transforms a Future that may fail into something that is safe to read.

We can use guard to simplify it:

  Future<void> sideEffect() async {
    state = const AsyncValue.loading();
    // does the try/catch for us like previously
    state = await AsyncValue.guard(() async {
      final response = await dio.get('my_api/data');
      return Data.fromJson(response);
    });
  }

Implementation

static Future<AsyncValue<T>> guard<T>(Future<T> Function() future) async {
  try {
    return AsyncValue.data(await future());
  } catch (err, stack) {
    return AsyncValue.error(err, stack);
  }
}