Flutter:按时间戳查询Firestore文档

jpv*_*jen 2 flutter google-cloud-firestore

我正在尝试通过时间戳字段(例如开始日期和结束日期之间)查询Firestore 文档。时间戳字段返回类似Timestamp(seconds=1599170400, nanoseconds=0).

我的应用程序中有一个 Unix 时间戳(以秒为单位),我喜欢将其与(使用isLessThan:)进行比较,但 Firestore 中的值显然位于此 Timestamp 对象内。

我将如何比较这两个时间戳?

另一种方法是查询所有文档并随后进行比较,但我想在查询本身中过滤文档。

Fra*_*len 7

要将Timestamp数据库中的 a 与自纪元以来的毫秒值进行比较,您可以Timestamp根据该值创建一个对象:

var timestamp = Timestamp.fromMillisecondsSinceEpoch(1599573193925);
Run Code Online (Sandbox Code Playgroud)

然后您可以在针对数据库中的值的查询中使用它:

Firestore.instance
.collection('items')
.orderBy('Timestamp', descending: true).startAt([timestamp])
Run Code Online (Sandbox Code Playgroud)

或者(我常用的方法):

Firestore.instance
.collection('items')
.where("Timestamp", isGreaterThanOrEqualTo: timestamp)
Run Code Online (Sandbox Code Playgroud)