如何通过与Flutter Cloud Firestore插件中的日期进行比较来获取记录? [英] How to fetch records by comparing with date in flutter cloud firestore plugin?

查看:122
本文介绍了如何通过与Flutter Cloud Firestore插件中的日期进行比较来获取记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个flutter应用程序上工作,我在其中使用Firebase Cloud Firestore存储所有文档。我的文档有一个 startDateTime 字段,其格式如下...

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

我想获取所有startDateTime大于或等于当前日期的记录。这是我要执行的操作代码。

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()}" ));
    })

但这不起作用。无法理解如何使用 isGreaterThanOrEqualTo 使其正常运行。

But this is not working. Can't able to understand how to use isGreaterThanOrEqualTo so that it'll work.

需要一些指导。

推荐答案

仅通过此查询

.where( trip.startDateTime,isGreaterThanOrEqualTo:new DateTime.now())

因为firebase自动将dart DateTime 对象转换为Timestamp Firebase对象。保存对象时,您还需要执行相同的操作。

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.

如果此方法不起作用,则可以将另一个对象保存为数字形式格式。然后很容易比较。您可以这样获得数字dateTime:

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

这篇关于如何通过与Flutter Cloud Firestore插件中的日期进行比较来获取记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆