decrementItem method

void decrementItem(
  1. 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:

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();
}