如何在带有Firestore的扑打中使用WhereIn? [英] How to use WhereIn in flutter with firestore?
问题描述
在一个电子商务扑扑的应用程序项目中,我将我的商品集合的 shortInfo
字段用作每个产品的唯一ID.当按下添加到购物车时,它将 shortInfo
保存到我的 users
集合中名为 userCartList
的数组字段中,并且我正在使用WhereIn查询此数组但是当我有10种以上的产品时,我的应用就会崩溃.我在这里做错什么了?
In an ecommerce flutter app project I am using the shortInfo
field of my items collection as an unique id for each product. When add to cart is pressed it saves the shortInfo
to an array field called userCartList
at my users
collection and I am using WhereIn to query this array but when I have more than 10 product my app crashes. What am I doing wrong here?
这是我的消防站结构:
以下是我正在执行的查询的摘要:
Here is a snippet of the query I am making:
StreamBuilder<QuerySnapshot>(
stream: EcommerceApp.firestore
.collection("items")
.where("shortInfo", whereIn: EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList))
.snapshots()
)
推荐答案
此处的问题是Firestore不允许您进行包含10条以上记录的数组成员资格查询.如果您查看此文档,则您会看到:
The problem here is that Firestore does not allow you to make an array membership query with more that 10 records. If you check this documentation you will see that:
使用
in
运算符将同一字段上的多达10个相等(==)子句与逻辑OR组合
Use the
in
operator to combine up to 10 equality (==) clauses on the same field with a logical OR
在后台,您的 whereIn
运算符进行的查询与文档示例中所示的查询相同,并且如果您使用10条以上的记录,则Firestore将不接受该记录,并且会出现错误.我建议您将 EcommerceApp.userCartList
数组拆分为最多具有10条记录的多个数组以进行查询,这将使Firestore可以在其限制范围内操作 whereIn
,它会起作用.
Behind the scenes your whereIn
operator is making the same query showed in the example in the documentation and if you use more than 10 records, Firestore will not accept it and you will get an error. I suggest that you break the EcommerceApp.userCartList
array into multiple arrays with a max of 10 records to make the query, this will allow Firestore to operate the whereIn
within it's limits and it will work.
让我知道您是否对此事还有其他疑问.
Let me know if you have any more questions on this matter.
这篇关于如何在带有Firestore的扑打中使用WhereIn?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!