asyncOperation method
Future<T?>
asyncOperation(
- FutureFunction<
T> futureFunction, { - bool? reloading,
- SuccessCallBack<
T> ? onSuccess, - VoidCallback? onDone,
- ErrorCallBack? onError,
- bool throwError = false,
- bool useCache = true,
Perform async function in manager
Implementation
@override
Future<T?> asyncOperation(
FutureFunction<T> futureFunction, {
bool? reloading,
SuccessCallBack<T>? onSuccess,
VoidCallback? onDone,
ErrorCallBack? onError,
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;
}
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) {
FutureManagerError error = FutureManagerError(
exception: exception,
stackTrace: stackTrace,
);
///Only update viewState if [triggerError] is true
addError(error, updateViewState: triggerError);
errorCallBack?.call(error);
if (shouldThrowError) {
rethrow;
}
return null;
} finally {
onOperationDone?.call();
}
};
return refresh(
reloading: reloading,
onSuccess: onSuccess,
onDone: onDone,
onError: onError,
throwError: throwError,
useCache: useCache,
);
}