addTask static method

Future<void> addTask(
  1. Product minimalistProduct, {
  2. required BuildContext? context,
  3. required BackgroundTaskDetailsStamp stamp,
  4. required ProductType? productType,
  5. LocalDatabase? localDatabase,
  6. bool showSnackBar = true,
})

Adds the background task about changing a product.

The typical use-case is that the user changes a product live. That's where context is not null. Another rarer case is when we change a product in stealth mode. That's where localDatabase is not null. At least one of context and localDatabase must be not null.

Implementation

static Future<void> addTask(
  Product minimalistProduct, {
  required BuildContext? context,
  required BackgroundTaskDetailsStamp stamp,
  required ProductType? productType,
  LocalDatabase? localDatabase,
  bool showSnackBar = true,
}) async {
  if (BackgroundTaskBarcode.isBarcodeToBeIgnored(
    minimalistProduct.barcode!,
    context,
  )) {
    return;
  }
  assert(context != null || localDatabase != null);
  localDatabase ??= context!.read<LocalDatabase>();
  final String uniqueId = await _operationType.getNewKey(
    localDatabase,
    barcode: minimalistProduct.barcode,
  );
  final BackgroundTaskBarcode task = _getNewTask(
    minimalistProduct,
    uniqueId,
    stamp,
    productType ?? ProductType.food,
  );
  if (context != null && context.mounted) {
    return task.addToManager(
      localDatabase,
      context: context,
      showSnackBar: showSnackBar,
      queue: BackgroundTaskQueue.fast,
    );
  }
  return task.addToManager(
    localDatabase,
    context: null,
    showSnackBar: false,
    queue: BackgroundTaskQueue.fast,
  );
}