addItem method
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 Codename: Display name for the productcategory: Raw category string (will be canonicalized)
Returns true if a new line was created, false if only quantity increased.
Side effects:
- Calls
_ensureCategoryInitto set default caps - Calls
_persistto save to FirebaseFirestore - Calls notifyListeners to update UI
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;
}