Flutter firestore - Check if document ID already exists

Solution 1:

You can use the get() method to get the Snapshot of the document and use the exists property on the snapshot to check whether the document exists or not.

An example:

final snapShot = await FirebaseFirestore.instance
  .collection('posts')
  .doc(docId) // varuId in your case
  .get();

if (snapShot == null || !snapShot.exists) {
  // Document with id == varuId doesn't exist.

  // You can add data to Firebase Firestore here
}

Solution 2:

Use the exists method on the snapshot:

final snapShot = await FirebaseFirestore.instance.collection('posts').doc(varuId).get();

   if (snapShot.exists){
        // Document already exists
   }
   else{
        // Document doesn't exist
   }

Solution 3:

To check if document exists in Firestore. Trick is to use .exists method

FirebaseFirestore.instance.doc('collection/$docId').get().then((onValue){
  onValue.exists ? // exists : // does not exist ;
});