kajembe
kajembe

Reputation: 25

How to check if array value exist in a document?

see firestore image here

i have document in firestore which has array of products now am struggling how to check if products already exist in such array so that user should not add same product multiple times.how to check for the exixtance of product in that array.

Upvotes: 0

Views: 390

Answers (1)

TobyLoby
TobyLoby

Reputation: 342

await Firestore.instance.collection('COLLECTION_NAME').document('TESTID1').get().then((doc) {
    if (!doc.data()["usercart"].contains(something)) {
        // add to cart
    }
});

You could also store a local array of the cart when you first fetch them. That way there is no need to read from the database. However, this could be problematic if multiple users are logged in to the same account, but can easly be avoided by using a stream.

Upvotes: 2

Related Questions