Aleja
Aleja

Reputation: 37

FieldValue.arrayUnion: How to add the same elements to an array? Flutter

I have created a review system, in a collection, within a document. In the document I have an array where the average of the ratings is (which when uploading the review are added), but if I upload two equal numbers with FieldValue.arrayUnion, I only get one.

Upvotes: 0

Views: 1370

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317352

That's the expected behavior of FieldValue.arrayUnion(). According to the API documentation:

Each specified element that doesn't already exist in the array will be added to the end. If the field being modified is not already an array it will be overwritten with an array containing exactly the specified elements.

FieldValue.arrayUnion() will not add an element if it already exists in the array. If you need to do that, you should read the document, modify the array in memory to contain what you want, then update the entire array back to the document.

Upvotes: 4

Related Questions