从 Firestore 集合构建的颤振流生成器中的多个 where 案例



我找不到如何在 Firestore Collection 返回的 Flutter Stream Builder 中使用多个 where 子句的文档或示例。

波纹管查询有效

Firestore
.instance
.collection (collectionName)
.where      ("index",   arrayContains:  search)
.orderBy    ('date',    descending:     true  )
.snapshots()

但是如果我添加一个额外的 where 子句,则返回零

Firestore
.instance
.collection (collectionName)
.where      ("index",   arrayContains:  search)
.where      ('allowed_roles', arrayContains: GlobalVars.userRole.toString() )
.orderBy    ('date',    descending:     true  )
.snapshots() 

Firebase 文档中的复合查询示例与上述内容非常相似,但缺少 Flutter 或 Dart 示例。

编辑: 我也尝试了查询构建模式作为解决方法,但它也不起作用。Flutter 真的不想要arrayContains条款不止一次。

CollectionReference collectionRef = Firestore.instance.collection (collectionName);
Query p =  collectionRef.where      ('allowed_roles', arrayContains: "3");
Query q =  p.where      ('index', arrayContains: "Dan");
Stream s = q.snapshots(); 

我在这里缺少的是程序员应该为这个问题使用什么解决方案,一个逻辑 AND 是一个非常基本的方程式,坚持这个功能是不寻常的。

一个查询中不能有两个 array包含筛选器:

.where      ("index",   arrayContains:  search)
.where      ('allowed_roles', arrayContains: GlobalVars.userRole.toString() )

根据文档,一次只支持一个 arrayContains。 这是 Firestore 的限制,无法更改。查询肯定会生成错误 - 您应该添加代码来检查错误,而不是盲目地假设它成功。

正如评论中提到的,Firebase 不需要多个 arrayContains 子句。

对于我的情况,唯一的解决方案是在我的活动中为数组挂allow_roles条件语句:

if( document['allowed_roles'].contains( GlobalVars.userRole ))

最新更新