Nour Nasser
Nour Nasser

Reputation: 1

i have a question about how to calculate a total price in the cart when the item is removed

i can calculate total price using this function and it works fine .. but it only update the total in the screen when item is added, when item is removed i have to refresh the screen, i want it to be displayed automatically when item is removed.

totall() async {
    totalPrice = 0;
    var Ref = FirebaseDatabase.instance.ref().child("carts").child("CartCode");
    var snapshot = await Ref.get();
    snapshot.children.forEach((element) {
    ItemModel model = ItemModel.fromJson(element.value);
    print('modelPrice: ${model.price}');

      setState(() {
        totalPrice += double.parse(model.price);
      });
    });
  }

Upvotes: 0

Views: 238

Answers (1)

Gejaa
Gejaa

Reputation: 104

Make sure you are rebuilding the state in the removal interaction also if so and not getting values I think the below will work.

In place of getting you can listen like var snapshot = await Ref.listen();

Upvotes: 1

Related Questions