【问题标题】:Firebase pagination and filtering at the same time in Flutter在 Flutter 中同时进行 Firebase 分页和过滤
【发布时间】:2020-12-18 13:26:12
【问题描述】:

我有一组可以从 Firebase (Cloud Firestore) 查询的办公室,然后我只需要显示距离用户一定距离内(比如说 20 英里)的办公室。我无法使用 db.collection('offices').where(...) 进行此查询。

我必须查询所有办公室,然后计算每个办公室与当前用户的距离,如果小于 20 英里,我会列出用户的办公室。但问题是我的收藏中可能有数百万个办公室,这意味着对于每个用户,我读取数百万个数据以显示可能只有不到 100 个办公室,这些办公室将在他/她的 20 英里范围内。

这里有 2 个问题 - 1) 我如何执行此查询而无需阅读此集合中的所有文档。 2)我如何对结果进行分页,以便即使有 100 个办公室落在所需的距离内,我仍然希望分批显示 10 个(获取前 10 个过滤的文档,一旦用户滚动到最后,获取下一个10 等等)。

【问题讨论】:

  • 谢谢。就一个问题。当我使用“geo.collection(collectionRef:collectionReference).within(center:center,radius:radius,field:field)”查询地理数据时,它不算作文档读取。对吗?
  • 另外,您知道我如何将此查询与正常的 firebase 查询(使用 ref.where)结合起来,以在 20 英里范围内找到“open”字段值为 true 的办公室吗?
  • 如果您还有其他问题,请单独发布。

标签: firebase flutter filter google-cloud-firestore pagination


【解决方案1】:

您可以使用 geolocator 包存储办公室的经纬度,然后使用 where 子句过滤位置。 这篇文章包含如何实现地理定位器包: Obtain coordinates from an address flutter

获取并保存每个办公室的地理定位器后,您可以使用两个 where 子句查询它,如下所示:

// Get user's latitude and longitude from current location and then 
var lat = userLatitude;
var long = userLongitude;

Firestore.instance
    .collection('addresses')
    .where('latitude', isLessThanOrEqualTo: lat)
    .where('longitude', isLessThanOrEqualTo: long)
    .getDocuments()
    .then(snapshots => {
      // Do Stuff here
    });

【讨论】:

  • 我不认为这会给我想要的结果。我想要用户半径 20 英里内的办公室!我猜你的解决方案是给我用户位置南面和西面的所有办公室!
猜你喜欢
  • 2015-08-12
  • 2019-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
  • 2019-03-22
  • 1970-01-01
  • 2019-09-22
相关资源
最近更新 更多