**Hey folks, I’m stuck with a problem in my online store app. I’m trying to let users change the number of items they want to buy. But when they add stuff to their cart, it just makes copies of the same item instead of updating the quantity. It’s driving me crazy!
Here’s what I’ve got so far:
ElevatedButton(
onPressed: () => updateCartItems(context, productInfo),
child: Row(
children: [
Text('Add to Basket'),
Icon(Icons.add_shopping_cart),
],
),
)
void updateCartItems(BuildContext context, ProductInfo productInfo) async {
int result = await shoppingService.updateBasket(productInfo);
if (result > 0) {
showMessage('Item added successfully!');
} else {
showMessage('Oops! Something went wrong.');
}
}
Future<int> updateBasket(ProductInfo productInfo) async {
var existingItems = await dbHelper.getItems('basket', 'itemId', productInfo.id);
if (existingItems.isNotEmpty) {
productInfo.amount = existingItems[0]['itemAmount'] + 1;
return await dbHelper.updateItem('basket', 'itemId', productInfo.toMap());
}
productInfo.amount = 1;
return await dbHelper.saveItem('basket', productInfo.toMap());
}
Any ideas on how to fix this? I’d really appreciate some help!
Hey Max_31Surf! I totally get your frustration. Shopping carts can be tricky little beasts, right? 
I’m curious about a couple things in your code. Have you checked if the updateBasket
function is actually being called when you press the button? Sometimes these sneaky callback issues can trip us up!
Also, it looks like you’re using a database to store the cart items. That’s cool! But I wonder, have you tried logging the contents of existingItems
after you fetch them from the database? It might give us a clue about what’s going on.
Oh, and one more thing - how are you displaying the cart contents to the user? Maybe the issue is in how it’s being rendered rather than how it’s stored?
Keep us posted on what you find out! We’re all learning here, and I’m super interested to see how you solve this. Good luck, mate!
yo max, i feel ya pain! shopping carts can be a real headache. looks like ur almost there tho. have u tried checking if the item exists in the cart before adding? maybe somethin like:
if (itemExists) {
updateQuantity();
} else {
addNewItem();
}
that might help ya out. good luck dude!
I’ve encountered a similar issue in my e-commerce projects. The problem likely lies in your database operations. Instead of updating the existing item, it appears you’re creating a new entry each time.
To resolve this, modify your updateBasket function to use an upsert operation. This will update the quantity if the item exists, or insert a new record if it doesn’t.
Consider implementing a separate function for incrementing quantity. This would allow for more granular control over cart operations.
Also, ensure your UI accurately reflects the database state. Sometimes, the issue isn’t in the data storage but in how it’s displayed to the user.
Lastly, implement proper error handling and logging in your updateBasket function. This will help you identify any underlying issues more effectively.