execute method
Future<T?>
execute(
- FutureFunction<
T> futureFunction, { - bool? reloading,
- SuccessCallBack<
T> ? onSuccess, - VoidCallback? onDone,
- ErrorCallBack? onError,
- bool throwError = false,
- bool useCache = true,
Implementation
Future<T?> execute(
FutureFunction<T> futureFunction, {
bool? reloading,
SuccessCallBack<T>? onSuccess,
VoidCallback? onDone,
///A callback called after exception is caught
///You can return `FutureManagerError` to override existing error
///Or return `false` to ignore the error and keep the current state
ErrorCallBack? onError,
///Throw error instead of catching it
bool throwError = false,
bool useCache = true,
}) async {
refresh = ({
reloading,
onSuccess,
onDone,
onError,
throwError = false,
useCache = false,
}) async {
bool shouldReload = reloading ?? this.reloading;
SuccessCallBack<T>? successCallBack = onSuccess ?? this.onSuccess;
ErrorCallBack? errorCallBack = onError ?? this.onError;
VoidCallback? onOperationDone = onDone ?? this.onDone;
bool shouldThrowError = throwError;
//useCache is always default to `false` if we call refresh directly
if (_enableCache && useCache) {
return data;
}
//
bool triggerError = true;
if (hasDataOrError) {
triggerError = shouldReload;
_reload = shouldReload;
}
try {
await resetData(updateViewState: shouldReload);
future = futureFunction.call();
T result = await future!;
if (successCallBack != null) {
result = await successCallBack.call(result);
}
updateData(result);
return result;
} catch (exception, stackTrace) {
var error = FutureManagerError(
exception: exception,
stackTrace: stackTrace,
);
var errorCallbackResult = await errorCallBack?.call(error);
if (errorCallbackResult is FutureManagerError) {
error = errorCallbackResult;
}
///Only add error if result isn't false
if (errorCallbackResult != false) {
///Only update viewState if [triggerError] is true
addError(error, updateViewState: triggerError);
if (shouldThrowError) {
rethrow;
}
} else if (hasData) {
updateData(data);
}
return null;
} finally {
_reload = false;
onOperationDone?.call();
}
};
return refresh(
reloading: reloading,
onSuccess: onSuccess,
onDone: onDone,
onError: onError,
throwError: throwError,
useCache: useCache,
);
}