首页 > 解决方案 > 如何在分页flutter firestore之前过滤数据

问题描述

我正在申请租车。我正在通过分页(限制 3)过滤它们以降低价格,但在某些情况下,公司可以提供折扣。发生这种情况时,我的数据库过滤变得无效。例如,数据库中有 4 辆汽车(40 美元、50 美元、60 美元、70 美元)。当我使用此代码过滤数据时:

var availableCars = await _firestore
        .collection('carMini')
        .where('locations', arrayContains: order.address)
        .orderBy('carPrice', descending: false)
        .limit(3)
        .get();

即使第 4 辆车的折扣价低于其他车,这个价格也会出现较晚,因为数据带有分页。有什么办法可以预先设置吗?

标签: firebasefluttergoogle-cloud-firestore

解决方案


Firestore 查询无法过滤此类计算值。使单个查询正常工作的唯一方法是将有效/折扣价格存储在单独的字段中并对其进行排序/过滤。

var availableCars = await _firestore
    .collection('carMini')
    .where('locations', arrayContains: order.address)
    .orderBy('discountedCarPrice')
    .limit(3)
    .get();

推荐阅读