代码之家  ›  专栏  ›  技术社区  ›  Mozes Ong

带有flutter的timetemp的firestore查询

  •  0
  • Mozes Ong  · 技术社区  · 6 年前

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

    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(),
    

    当我将降序设置为true时,我将返回所有文档。 当我设为假的时候,我一个也得不到。 两者都不是预期的结果。

    我在堆栈上看到一些命令将日期时间转换为毫秒或Unix。但我使用服务器时间戳是因为我不能信任客户端来编写时间戳。知道如何用服务器时间戳查询日期时间范围吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   dshukertjr    6 年前

    我不能解释为什么这样做,但这对我有效。

    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()