How to fetch records by comparing with date in flutter cloud firestore plugin?

14,840

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.

enter image description here

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

Share:
14,840

Related videos on Youtube

Suresh
Author by

Suresh

Full-Stack Software Engineer having 10Yrs of industrial experience in developing large scale web/mobile applications. Indian by born, currently staying in UAE. Nowadays, offering my expertise from my couch. I can help you in building your website create your MVP or web/mobile application, if you have any product idea design a custom solution to automate your business process On off days I love to travel, meet new people & capture the beauty of this world.

Updated on July 09, 2022

Comments

  • Suresh
    Suresh almost 2 years

    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...

    enter image description here

    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.

  • Suresh
    Suresh almost 6 years
    Oh man, seems like my brain is not with me. Actually, I tried new DateTime.now() also but that didn't work & the Silly issue was... I don't have any document in my firebase whose startDateTime time was higher than today's date. Anyway, thanks for this reply.
  • Rachit Rawat
    Rachit Rawat over 5 years
    It works on android but not on ios. Please help. @Tree
  • Tree
    Tree over 5 years
    what do you get on iOS?
  • Uday
    Uday almost 2 years
    How to do this same logic using javascript? @Tree