1

hi guys i am trying to develop an app but i need some help i have a function i just need to do if item is exist on firestore i need to increment "quantity" value of it. i already got and printing the value but i need some help for checker and incrementing value thanks

String checker = await Firestore.instance
        .collection('cart')
        .document('LIihBLtbfuJ8Dy640DPd')
        .get()
        .then((DocumentSnapshot ds) => ds.data[foodItem.name]);
    if (checker == foodItem.name) {
      await Firestore.instance
          .collection('cart')
          .document('LIihBLtbfuJ8Dy640DPd')
          .get()
          .then((DocumentSnapshot ds) =>
              a = ds.data[foodItem.name]['quantity']);

      Firestore.instance
          .collection("cart")
          .document("LIihBLtbfuJ8Dy640DPd")
          .updateData({
        foodItem.name: {'quantity': q++}
      });
    } else {
      Firestore.instance.runTransaction((Transaction transaction) async {
        await transaction.update(
            Firestore.instance
                .collection("cart")
                .document("LIihBLtbfuJ8Dy640DPd"),
            {
              foodItem.name: {
                'itemName': foodItem.name,
                'imgUrl': foodItem.imageAssetPath,
                'itemPrice': foodItem.price,
                'quantity': q,
              }
            });
      });
    }

enter image description here this is picture of my firestore as you can see there is nested nodes there

1

1 Answer 1

2

This is what you want to increment a value if the field doesn't exist firebase will create it with the value of 1. I am confused as to the checker part of your question.

Firestore.instance.collection("cart")
.document("LIihBLtbfuJ8Dy640DPd")
.setData({foodItem.name: {"quantity": FieldValue.increment(1)}});
Sign up to request clarification or add additional context in comments.

7 Comments

Likely that needs to be: updateData({ 'foodItem.name.quantity': FieldValue.increment(1) }).
it does not work too because it is nested nodes by the way
okay I copy and pasted your code FieldValue.increment(1) works I tried it in my app
it works but it is outside of the nodes creating new data
it is not exactly changing my value it does not effect it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.