问题
Working on a flutter application where I'm using firebase cloud firestore for storing all my documents. My document has a startDateTime field having a format like this...
I want to fetch all records whose startDateTime is greater than or equal to current date. Here is my flutter code to do this...
Firestore.instance.collection("trips")
.where("trip.createdByUID", isEqualTo: this.userDetails['id'])
.where("trip.startDateTime", isGreaterThanOrEqualTo: new DateTime.now().toString() )
.getDocuments().then((string) {
print('Firestore response111: , ${string.documents.length}');
string.documents.forEach((doc) => print("Firestore response222: ${doc.data.toString()}" ));
})
But this is not working. Can't able to understand how to use isGreaterThanOrEqualTo so that it'll work.
Need some guidance.
回答1:
Pass just this query
.where("trip.startDateTime", isGreaterThanOrEqualTo: new DateTime.now())
Since firebase automatically converts dart DateTime object to Timestamp Firebase object. You also need to do the same when you save the object.
You can see on the right if the object type is timestamp.
In case this doesn't work, you can save another object on your creation in numeric format. Then it is easy to compare. You can get numeric dateTime like this:
new DateTime.now().millisecondsSinceEpoch
回答2:
I got it working on iOS. Please check the code below.
DateTime _now = DateTime.now();
return StreamBuilder<QuerySnapshot>(
stream: _store
.collection('rides')
.orderBy('rideDate')
.orderBy('rideType')
.where('rideDate', isGreaterThan: _now)
.snapshots(),
来源:https://stackoverflow.com/questions/50663556/how-to-fetch-records-by-comparing-with-date-in-flutter-cloud-firestore-plugin