首页 > 解决方案 > Flutter Cloud Firestore isLessThan 过滤器不使用两个 .where() 语句

问题描述

我正在使用StreamBuilder. 目前,我可以使用.where()语句按标题(字符串)过滤此文档列表,但我想添加另一个.where()语句按价格(数字)过滤。我希望该应用程序仅显示标题等于某个字符串且价格小于某个数字的文档列表。按标题过滤可以正常工作,但问题在于isLessThan添加第二条.where()语句时的操作员。

当前代码(一条.where()语句):

StreamBuilder<QuerySnapshot>(
              stream: FirebaseFirestore.instance
                  .collection("ads")
                  .where('title', isEqualTo: "Car")
                  .snapshots(),
              builder: (context, snapshot) {
                if (!snapshot.hasData) {
                  return Text(
                      "Loading...");
                } else {
                  return //Rest of the code isn't relevant
                }
              },
            ),

我想使用两个.where()语句的代码(它只返回加载文本,这意味着快照没有任何数据):

StreamBuilder<QuerySnapshot>(
              stream: FirebaseFirestore.instance
                  .collection("ads")
                  .where('title', isEqualTo: "Car")
                  .where('price', isLessThan: 12000.0) //This line is new
                  .snapshots(),
              builder: (context, snapshot) {
                if (!snapshot.hasData) {
                  return Text(
                      "Loading...");
                } else {
                  return //Rest of the code isn't relevant
                }
              },
            ),

我无法确定这是否是存在两个.where()语句的问题,或者是否是isLessThan操作员的问题。删除第一个声明(关于标题的声明)并保留第二个关于价格的声明,按价格正确过滤文档(理论上使问题成为有 2 个声明.where()的事实)。.where()但是,将第二isLessThan.where()语句中的 这让我认为将运算符和第二条语句结合起来是一个问题。isEqualTo12000.0.where()isLessThan.where()

注意:Firestore 集合中的所有价格值都是双精度值(Cloud Firestore 中的数据类型设置为“数字”并且它们都有小数 - 例如 1.0 而不是 1)。

标签: firebasefluttergoogle-cloud-firestore

解决方案


推荐阅读