[英]Getting data from firebase with where using flutter
I will pull data from firebase according to the search word, but I had a problem using "where", how can I solve it?我会根据搜索词从firebase中拉取数据,但是我在使用“where”时遇到了问题,我该如何解决?
i want to filter by value under name我想按名称下的值过滤
FirebaseFirestore.instance
.collection('tedaviler')
.where("value.name", arrayContains: name)
.snapshots()
this is not working这是行不通的
Firestore's array-contains
operation tests whether the value that you pass exists as an item in the array you identify. Firestore 的
array-contains
操作会测试您传递的值是否作为您标识的数组中的一项存在。 The value you pass must match the complete item in the array, there is no option to check just some of the properties.您传递的值必须与数组中的完整项目相匹配,没有仅检查某些属性的选项。
In your code you have a condition:在您的代码中,您有一个条件:
.where("value.name", arrayContains: name)
This means you're looking for a field value
at the root of the document, and then a subfield name
under that.这意味着您要查找文档根部的字段
value
,然后是该字段下的子字段name
。 But your value
field is an array, and it doesn't have a subfield name
.但是您的
value
字段是一个数组,并且没有子字段name
。 Each item in the array may have a name
subfield, but that is not the same.数组中的每个项目都可能有一个
name
子字段,但那是不一样的。
So the condition is never met, and your query returns no documents.因此永远不会满足条件,您的查询不会返回任何文档。
If you want to test the items in the value
array, you will have to know the entire item.如果要测试
value
数组中的项目,则必须了解整个项目。 If you have that, the condition becomes:如果你有那个,条件就变成了:
.where("value", arrayContains: {
"id": "IzotonikId123",
"name": "IV Izotonik %1",
"price": 8.88
})
If you want to be able to search on just the names, you'll need to add another array field to the document, where you keep just the (unique) value names:如果您希望能够仅搜索名称,则需要向文档添加另一个数组字段,其中仅保留(唯一)值名称:
valueNames: ["IV Izotonik %1", "IV Izotonik %2", "IV Izotonik %3"]
Then you can query for the existing of a specific name with:然后您可以查询特定名称的存在:
.where("valueNames", arrayContains: name)
name
is not an array in your DB. name
不是您数据库中的数组。 It is a string.它是一个字符串。 You should try
你应该试试
FirebaseFirestore.instance
.collection('tedaviler')
.where("value.name", isEqualTo: name)
.snapshots()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.