decrementItem method
- String upc
Decreases the quantity of an existing basket item by 1.
If the new quantity reaches 0, the entire item is removed from basket. Updates the category's used count in balances accordingly.
Does nothing if:
- No user is logged in
- Item not found in basket
Side effects:
- Calls
_persistto save to FirebaseFirestore - Calls notifyListeners to update UI
Implementation
void decrementItem(String upc) {
if (_uid == null) return;
final i = basket.indexWhere((e) => e['upc'] == upc);
if (i < 0) return;
final cat = _canon(basket[i]['category'] as String);
final newQty = (basket[i]['qty'] ?? 1) - 1;
if (balances.containsKey(cat)) {
final used = (balances[cat]!['used'] ?? 0) as int;
balances[cat]!['used'] = (used - 1).clamp(0, 999);
}
if (newQty <= 0) {
basket.removeAt(i);
} else {
basket[i]['qty'] = newQty;
}
// ignore: discarded_futures
_persist();
notifyListeners();
}