easyFuture<Z> method

Future easyFuture<Z>({
  1. required Future<Z> future(),
  2. dynamic returnOnError()?,
  3. bool showSnackBarOnError = false,
  4. String snackBarMessage(
    1. Exception e
    )?,
  5. Color? snackBarColor = Colors.red,
  6. ScaffoldFeatureController<Widget, dynamic> snackbar(
    1. Exception e
    )?,
  7. bool indicatorWhileFuture = false,
  8. bool restrictedIndicator = true,
  9. Color? indicatorColor,
})

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;
}