如何在 flutter 和 firestore 中使用WhereIn?

Bas*_*yak 6 firebase flutter google-cloud-firestore

在电子商务 flutter 应用程序项目中,我使用shortInfo项目集合的字段作为每个产品的唯一 ID。当按下“添加到购物车”时,它会将其保存到在我的集合中shortInfo调用的数组字段,我正在使用WhereIn来查询该数组,但是当我有超过10个产品时,我的应用程序崩溃了。我在这里做错了什么?userCartListusers

这是我的 Firestore 结构:

用户收藏

物品收集

这是我正在执行的查询的片段:

StreamBuilder<QuerySnapshot>( 
    stream: EcommerceApp.firestore
                        .collection("items")
                        .where("shortInfo", whereIn: EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList))
                        .snapshots()
)
Run Code Online (Sandbox Code Playgroud)

Ral*_*mos 7

这里的问题是 Firestore 不允许您进行包含超过 10 条记录的数组成员资格查询。如果您检查此文档,您将看到:

使用in运算符通过逻辑 OR 组合同一字段上最多 10 个等式 (==) 子句

您的操作员在后台whereIn进行与文档示例中所示的相同查询,如果您使用超过 10 条记录,Firestore 将不会接受它,您将收到错误消息。我建议您将EcommerceApp.userCartList数组分成多个数组,最多包含 10 条记录来进行查询,这将允许 FirestorewhereIn在其限制内进行操作并且它将起作用。

如果您对此事还有其他疑问,请告诉我。

  • 我能够使用以下代码解决此问题: for (int i = 1; i &lt; EcommerceApp.sharedPreferences .getStringList(EcommerceApp.userCartList) .length; i++) StreamBuilder&lt;QuerySnapshot&gt;( stream: EcommerceApp.firestore .collection(" items") .where("shortInfo", isEqualTo: EcommerceApp.sharedPreferences .getStringList(EcommerceApp.userCartList)[i]) .snapshots(), (4认同)