scott lee
scott lee

Reputation: 646

How to increment individual element in an array in firestore for flutter

enter image description here This is how my data look like.

I'm able to use FieldValue.increment(1) to update individual fields but is there a way to use FieldValue.increment(1) for specific individual elements in the array? Thanks in advance!

I tried using the following code:

firestore.collection('test').doc('I1raMaJArb1sWXWqQErE')
          .update({
        'rating.0': FieldValue.increment(1),
      });

But the whole rating became empty as seen enter image description here

Upvotes: 0

Views: 342

Answers (1)

JM Gelilio
JM Gelilio

Reputation: 3768

You can't use FieldValue.increment() if the field is part of the array. The value of the field inside the array is fixed. The best way to update or edit the field that part of an array is to read the entire document, modify the data in memory and update the field back into the document.

Upvotes: 1

Related Questions