fetchAndRefresh method

Future<bool> fetchAndRefresh({
  1. required String barcode,
  2. required BuildContext context,
})

Fetches the product from the server and refreshes the local database.

With a waiting dialog. Returns true if successful.

Implementation

Future<bool> fetchAndRefresh({
  required String barcode,
  required BuildContext context,
}) async {
  final LocalDatabase localDatabase = context.read<LocalDatabase>();
  final AppLocalizations appLocalizations = AppLocalizations.of(context);
  final FetchedProduct? fetchAndRefreshed =
      await LoadingDialog.run<FetchedProduct>(
        future: silentFetchAndRefresh(
          localDatabase: localDatabase,
          barcode: barcode,
        ),
        context: context,
        title: appLocalizations.refreshing_product,
      );
  if (fetchAndRefreshed == null) {
    // the user probably cancelled
    return false;
  }
  if (fetchAndRefreshed.product == null) {
    if (context.mounted) {
      await LoadingDialog.error(
        context: context,
        title: fetchAndRefreshed.getErrorTitle(appLocalizations),
      );
    }
    return false;
  }
  if (context.mounted) {
    ScaffoldMessenger.of(context).showSnackBar(
      SmoothFloatingSnackbar.positive(
        context: context,
        text: appLocalizations.product_refreshed,
      ),
    );
  }
  return true;
}