addItem method

bool addItem({
  1. required String upc,
  2. required String name,
  3. required String category,
})

Adds one item to the basket and updates balances.

If an item with the same upc already exists, increments its quantity via incrementItem and returns false.

Otherwise, creates a new basket line with qty=1, increments the category's used count, and persists via _persist.

Parameters:

  • upc: Universal Product Code
  • name: Display name for the product
  • category: Raw category string (will be canonicalized)

Returns true if a new line was created, false if only quantity increased.

Side effects:

Implementation

bool addItem({
  required String upc,
  required String name,
  required String category,
}) {
  if (_uid == null) return false;
  final cat = _canon(category);

  _ensureCategoryInit(cat);
  if (!_canAddCanon(cat)) return false;

  final idx = basket.indexWhere((e) => e['upc'] == upc && upc.isNotEmpty);
  if (idx >= 0) {
    // existing line -> increment path
    incrementItem(upc);
    return false;
  }

  basket.add({'upc': upc, 'name': name, 'category': cat, 'qty': 1});
  balances[cat]!['used'] = (balances[cat]!['used'] ?? 0) + 1;

  // persist (fire-and-forget)
  // ignore: discarded_futures
  _persist();
  notifyListeners();
  return true;
}