I want to delete a document from collection in Firestore

Viewed 80

I want to delete a document from a collection in Firebase Firestore. I wrote a method, but it's not working and does not delete anything. I need help, and this is my method:

final couponsReference =  FirebaseFirestore.instance.collection("Coupons");

  Future<void> Deletecoupon() async {
// displayToastMassage('try1', context);


String s=couponsReference.doc().id;
couponsReference.doc(s).delete().catchError((s){
  print(s);
});
displayToastMassage('Coupn Code has been deleted sucsseflly', context);
2 Answers

To delete a document you need the documentId of that specific document. In your deletecoupon method you should pass in the documentId and could then use the await keyword to => await couponsReference.doc(documentId).delete();

To delete a particular document from a collection is very easy you just need one thing that documents documents I'd which you want to delete :

   FirebaseFirestore.instance .collection("blogs").doc(widget.postid).delete();
 
Related