i dont know what the issue in my code but when i remain 2 quantity and 200 price and go back and go to cart and i press add icon it show 400 price and 2 quantity but i want 3quantity and 300 price I think issue is in updating product price and quantity .I don't know what the error.Please anyone who solve the issue ? Here is my code
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
snapshot.data!.docs[index]["Productname"],
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
SizedBox(height: 8),
Row(
children: [
Text(
"${int.parse(snapshot.data!.docs[index]["productprice"].toString())*snapshot.data!.docs[index]["quantity"]}",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
SizedBox(width: 16),
IconButton(
icon: Icon(Icons.remove),
onPressed: ()async{
setState(() {
if (_quantity>1) {
_quantity--;
}
});
await FirebaseFirestore.instance
.collection('Cart')
.doc(snapshot.data!.docs[index].id) // Use the document ID as the unique identifier
.update({'quantity': _quantity});
},
),
SizedBox(width: 8),
Text(
"${snapshot.data!.docs[index]["quantity"]}",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
SizedBox(width: 8),
IconButton(
icon: Icon(Icons.add),
onPressed: ()async{
setState(() {
_quantity++;
});
await FirebaseFirestore.instance
.collection('Cart')
.doc(snapshot.data!.docs[index].id) // Use the document ID as the unique identifier
.update({'quantity': _quantity});
var newPrice = int.parse(snapshot.data!.docs[index]["productprice"].toString()) * snapshot.data!.docs[index]["quantity"];
await FirebaseFirestore.instance
.collection('Cart')
.doc(snapshot.data!.docs[index].id)
.update({'productprice': newPrice});
},
),
],
),
],
),
You are updating the productPrice
as well in the place of updating only quantity
You should remove following two lines in add button
var newPrice = int.parse(snapshot.data!.docs[index]["productprice"].toString()) * snapshot.data!.docs[index]["quantity"];
await FirebaseFirestore.instance
.collection('Cart')
.doc(snapshot.data!.docs[index].id)
.update({'productprice': newPrice});
Your add button code should be as following
IconButton(
icon: Icon(Icons.add),
onPressed: ()async{
setState(() {
_quantity++;
});
await FirebaseFirestore.instance
.collection('Cart')
.doc(snapshot.data!.docs[index].id) // Use the document ID as the unique identifier
.update({'quantity': _quantity});
},
)