首页 > 解决方案 > 带有颤动的timetemp的Firestore查询

问题描述

我正在像这样向我的收藏中添加一个文档。

void addToDb() {
collectionReference.add({
  'dateTime': FieldValue.serverTimestamp(),
});}

我正在尝试像这样在某个日期时间之后查询数据

Firestore.instance
                .collection('items')
                .orderBy('dateTime', descending: true).startAt([{'dateTime' : DateTime.parse('2019-03-13 16:49:42.044')}])
                .snapshots(),

当我将descending 设置为true 时,我会取回所有文档。当我设置为假时,我什么也没回来。两者都不是预期的结果。

我在堆栈上看到一些说将日期时间转换为毫秒或 unix。但我正在使用 servertimestamp 因为我不能信任客户端写时间戳。知道如何使用 servertimestamp 查询日期时间范围吗?

标签: firebasefluttergoogle-cloud-firestore

解决方案


我无法真正解释为什么会这样,但这对我有用。

final startAtTimestamp = Timestamp.fromMillisecondsSinceEpoch(DateTime.parse('2019-03-13 16:49:42.044').millisecondsSinceEpoch);
Firestore.instance
                .collection('items')
                .orderBy('dateTime', descending: true).startAt([startAtTimestamp])
                .snapshots()

推荐阅读