futurize method

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

Executes a future and automatically handles status changes.

This method sets the status to loading, executes the future, and then sets the status to success, error, or empty based on the result.

Implementation

void futurize(
  Future<T> Function() body, {
  T? initialData,
  String? errorMessage,
  bool useEmpty = true,
}) {
  final compute = body;
  _value ??= initialData;
  status = GetStatus<T>.loading();
  compute().then(
    (newValue) {
      if ((newValue == null || newValue._isEmpty()) && useEmpty) {
        status = GetStatus<T>.empty();
      } else {
        status = GetStatus<T>.success(newValue);
      }

      refresh();
    },
    onError: (err) {
      status = GetStatus.error(
        err is Exception ? err : Exception(errorMessage ?? err.toString()),
      );
      refresh();
    },
  );
}