I’m trying to delete some data with where
parameter to specify which one should be deleted.
I’ve this method and it works perfectly.
final FirebaseFirestore _db = FirebaseFirestore.instance;
Future deleteInstructorDocumentInFirestore(String url) async {
_db.collection(instructorsDocument)
.where("photo", isEqualTo: url)
.get()
.then((value) {
for (var element in value.docs) {
_db.collection(instructorsDocument)
.doc(element.id)
.delete();
}
});
}
So I’ve tried the same thing with this one but it doesn’t work.
Firestore
Function
final FirebaseFirestore _db = FirebaseFirestore.instance;
Future deleteEvent(String uid, DateTime from, DateTime to) async {
print("From: $from");
print("From type: ${from.runtimeType}");
print("To: $to");
print("To type: ${to.runtimeType}");
_db.collection(instructorsEvent)
.where("uid", isEqualTo: uid)
.where("from", isEqualTo: from)
.where("to", isEqualTo: to)
.get()
.then((value) {
for (var element in value.docs) {
_db.collection(instructorsEvent)
.doc(element.id)
.delete();
}
});
}
What am I doing wrong.
Thanks in advance
2
Answers
Problem Solved
@Doug Stevenson thanks
Utils
EventViewModel
The
from
andto
fields are strings. But you are trying to compare them to dart DateTime object, which are not strings. As such, they will not match in any query – your queries are simply returning no documents at all, so there is nothing to delete. (I suggest adding some debug logging to better observe this for yourself.)Firestore will not convert DateTime objects to strings for you, nor will it try to guess how to compare them for you. You must provide a similar type of value to the document field you are trying to compare to.
So, if your document fields must remain strings, then you should find a way to convert your DateTime objects to strings before handing them to a Firestore query. Or, you must find another way of representing points in time (in Firestore, usually a timestamp type field), then provide objects to the query that Firestore naturally understand to be comparable to timestamps.
See also: