【问题标题】:FireStore Error in Cursor - cursor position is outside the range光标中的 FireStore 错误 - 光标位置超出范围
【发布时间】:2019-06-21 20:01:44
【问题描述】:

我有一个方法可以从 FireStore 返回一些数据,该方法用于我的 Flutter 应用中的分页。

  static Future<QuerySnapshot> getAllItems({int count: 12, dynamic lastKey}) {

    if (lastKey != null) {
      return Firestore.instance
          .collection('uploads')
          .where('active', isEqualTo: true)
          .where('purge', isEqualTo: false)
          .startAfter([lastKey])
          .orderBy("active_time", descending: true)
          .where('active_time', isLessThanOrEqualTo: DateTime.now())
          .limit(count)
          .getDocuments();
    } else {
      return Firestore.instance
          .collection('uploads')
          .where('active', isEqualTo: true)
          .where('purge', isEqualTo: false)
          .orderBy("active_time", descending: true)
          .where('active_time', isLessThanOrEqualTo: DateTime.now())
          .limit(count)
          .getDocuments();
    }
  }

当没有传递 lastKey 值时,一切正常。但是当我传递 lastKey 的字符串值时,我得到了以下错误。

cursor position is outside the range of the original query

active_time 字段是 FireStore 中的时间戳类型。为相同创建索引。

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    由于active_time 是一个日期时间,lastKey 也应该是一个日期时间。

    来自startAfter上的文档

    获取一个值列表,创建并返回一个新的查询,该查询开始 在相对于查询顺序提供的字段之后。

    https://pub.dartlang.org/documentation/cloud_firestore/latest/cloud_firestore/Query/startAfter.html

    【讨论】:

    • 它已经是文档中提到的相同数据类型。
    • @Purus 我建议不要让它成为动态
    • 你能解释一下你的意思吗
    • getAllItems 接受一个名为 lastKey 的参数,它是动态类型的。它应该是 DateTime 类型。
    【解决方案2】:

    让我记录一下我遇到的问题。

    问题是由于数据类型的不同。 Firebase 数据库和我的 dart 变量类型不同……即……日期时间和时间戳。修复此问题后,代码开始工作。

    【讨论】:

      猜你喜欢
      • 2017-06-05
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多