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.
So today, in this article, we will learn about How to get all data from a Firestore collection in flutter. How to get all data from a Firestore collection in flutter? Call the getDocs () function, then use the build function, then print all the document IDs in the console. Here is how you do it!
There are two ways to add data to the Cloud Firestore, first way is to specifiy the document name and the second way Cloud Firestore will generate a random id, let us see both cases. So first in your State class you need to get an instance of Cloud Firestore:
We built a simple Flutter application that uses the Firestore database as a backend. You also learned how to perform the four most important tasks when working with a database: creating data, updating data, reading data, and deleting data. From now on, you can make more complicated and complex apps with that tech stack.
Firebase Firestore is a NoSQL database, which means the data we store, are organized in the form of collections rather than tables as of in other relational databases.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With