'Is there any way to make sure no duplicate items get added in firestore, I'm using Flutter and Firebase and I'm pretty new to this

   Future addCart() async{
     final FirebaseAuth _auth = FirebaseAuth.instance;
     var currentUser = _auth.currentUser;
     CollectionReference _collectionRef = FirebaseFirestore.instance.collection("users-cart- 
     items");
    return _collectionRef.doc(currentUser!.email).collection("items").doc().set(
      {
       "name": widget.name,
        "price": widget.price,
        "images": widget.image,
      }).then((value) => print("Added To Cart"));

     }

I'm using collectionReference and it does not have any .contain method to check if there are items of the same type already existing.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source