futurize method

Future<void> futurize(
  1. Future<T> body(), {
  2. T? initialData,
  3. String? errorMessage,
})

Fetches data asynchronously and updates the state accordingly.

The body function should return a Future which resolves to the new state.

Implementation

Future<void> futurize(
  Future<T> Function() body, {
  T? initialData,
  String? errorMessage,
}) async {
  final Future<T> Function() compute = body;
  _value ??= initialData;
  await compute().then((T newValue) {
    if (newValue == null) {
      status = GetStatus<T>.loading();
    } else {
      if (newValue._isEmpty()) {
        status = GetStatus<T>.empty();
      } else {
        status = GetStatus<T>.success(newValue);
      }
    }
    refresh();
  }, onError: (Object err) {
    status = GetStatus<T>.error(errorMessage ?? err.toString());
    refresh();
  });
}