首页 > 解决方案 > orderBy 上的 Firestore 查询崩溃?

问题描述

我正在使用 Flutter Firebase SDK。当我使用这个复合查询时,它会导致错误:

      .where('field', whereNotIn: [‘data’])
      .where('otherfield}',
          isEqualTo: anotherField),
     .orderBy(‘timestamp’, descending: false),

解析查询参数时发生错误,这很可能是此 SDK 的错误。(

如果我删除 orderBy,则不会崩溃。例如:

          .where('field', whereNotIn: [‘data’])
          .where('otherfield}',
              isEqualTo: anotherField),
  //       .orderBy(‘timestamp’, descending: false),

作品^

为什么?

感谢帮助!

标签: firebasefluttergoogle-cloud-firestore

解决方案


如果您包含具有范围比较(<、<=、>、>=)的过滤器,则您的第一个排序必须在同一字段上有效:

citiesRef.where("population", ">", 100000).orderBy("population")

无效的:

citiesRef.where("population", ">", 100000).orderBy("country")

我刚刚成功修复的错误示例错误:

.whereEqualTo("restaurantID", resRef)
.whereNotEqualTo("image", "")
.orderBy("createdAt", Query.Direction.DESCENDING)

正确的:

.whereEqualTo("restaurantID", resRef)
.whereNotEqualTo("image", "")
**.orderBy("image", Query.Direction.ASCENDING)**
.orderBy("createdAt", Query.Direction.DESCENDING).get()

https://firebase.google.com/docs/firestore/query-data/order-limit-data


推荐阅读