easyFuture<Z> method
Future
easyFuture<Z>({})
will throw an error if showSnackBarOnError
is true and snackbarMessage
is not given
Default snackbar will be used if snackbar is null
user will not be able to close the indicator with back button if restrictedIndicator
is true, recommended.
Implementation
Future<dynamic> easyFuture<Z>({
required Future<Z> Function() future,
Function()? returnOnError,
bool showSnackBarOnError = false,
String Function(Exception e)? snackBarMessage,
Color? snackBarColor = Colors.red,
ScaffoldFeatureController Function(Exception e)? snackbar,
bool indicatorWhileFuture = false,
bool restrictedIndicator = true,
Color? indicatorColor,
}) async {
assert(
(showSnackBarOnError && snackBarMessage.isNotNull) ||
(!showSnackBarOnError),
'showSnackerBarOnError is true but snackBarMessage is not provided');
if (indicatorWhileFuture)
indicatorColor.isNotNull
? easyColorLoader(
color: indicatorColor!, restricted: restrictedIndicator)
: restrictedIndicator
? easyRestrictedLoader
: easyLoader;
try {
Z toRet = await future();
if (indicatorWhileFuture) back;
return toRet;
} on Exception catch (e) {
if (indicatorWhileFuture) back;
if (showSnackBarOnError) {
snackbar.isNotNull
? snackbar!(e)
: easySnackBar(
snackBarMessage!(e),
backgroundColor: snackBarColor,
);
}
}
return returnOnError != null ? returnOnError() : null;
}