3

Context: In the frontend the user can enter of delete dates for availability. This is how I added a date:

Firestore.instance.collection('availableDates').add({
        'availableDates':date});

But I also need to delete a document from a collection in Firestore. I don't know the document id but I know the the field value which is unique. How would I go about deleting the document if I know the field value within the document. Here is a screen shot of firestore:

enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
flutter
  • 6,188
  • 9
  • 45
  • 78

2 Answers2

10

You can fetch the document first and then delete the document. For example:

var collection = FirebaseFirestore.instance.collection('countries');
var snapshot = await collection.where('city', isEqualTo: 'CA').get();
await snapshot.docs.first.reference.delete();
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
nick.tdr
  • 4,627
  • 4
  • 28
  • 41
2

If anyone is confused after the update of why this code is not working please refer to this one. Update of March 21st, 2021.

refUser.where("city", isEqualTo: "CA").get().then((snapshot){
snapshot.docs.first.reference.delete();
});

I took a bit of time myself to get around this but if it helps even a single person stuck like me then it's all worth it. :)

Raza Shabbir
  • 146
  • 5