'Displays the same cart-quantity and total price
I am having issue with the cart notification panel as it displays the product-quantity and price of the previous user and is not updated even after i add products from new user account.
This is the code for notification panel
Widget build(BuildContext context) {
final _cartProvider = Provider.of<CartProvider>(context);
_cartProvider.getCartTotal();
return Container(
height: 45,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: AppColors.buttonnavigation,
borderRadius: BorderRadius.only(
topRight: Radius.circular(18), topLeft: Radius.circular(18))),
child: Padding(
padding: const EdgeInsets.only(left: 10, right: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Center(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
'${_cartProvider.cartQty}${_cartProvider.cartQty == 1 ? 'Item' : 'Items'}',
style:
TextStyle(color: Colors.white, fontSize: 14)),
Text(
' | ',
style: TextStyle(color: Colors.white),
),
Text('Rs.${_cartProvider.subTotal}',
style:
TextStyle(fontSize: 18, color: Colors.white)),
],
),
],
),
),
Here is the Provider i've used
class CartProvider with ChangeNotifier {
final CartServices _cart = CartServices();
double subTotal = 0.0;
int cartQty = 0;
QuerySnapshot? snapshot;
List cartList = [];
Future<double?> getCartTotal() async {
var cartTotal = 0.0;
List _newList = [];
QuerySnapshot snapshot =
await _cart.cart.doc(_cart.user!.uid).collection('products').get();
if (snapshot == null) {
return null;
}
snapshot.docs.forEach((doc) {
if (!_newList.contains(doc.data())) {
_newList.add(doc.data());
cartList = _newList;
notifyListeners();
}
cartTotal = cartTotal + doc['total'];
});
subTotal = cartTotal;
cartQty = snapshot.size;
this.snapshot = snapshot;
notifyListeners();
return cartTotal;
}
}
I am not exactly sure what went wrong in this as i've just started in flutter and firebase.
Solution 1:[1]
You might want to check what documents you are listening to. From what you say, your app may still be listening to the old user's document and thus you see no change.
P.S.: I believe you should provide a minimal piece of code to recreate the issue you are facing and using pictures is not a very good way. You should try to add the code here as text instead.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Haany Ali |
