futurize method
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;
try {
final T newValue = await compute();
if (newValue == null) {
status = GetStatus<T>.loading();
} else if (newValue._isEmpty()) {
status = GetStatus<T>.empty();
} else {
status = GetStatus<T>.success(newValue);
}
} catch (error) {
status = GetStatus<T>.error(errorMessage ?? error.toString());
} finally {
refresh();
}
}