简体   繁体   English

使用 flutter 从 firebase 获取数据

[英]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.

相关问题 使用 Flutter Firebase Firestore 从数据库获取地理点数据 - Getting Geopoint Data from Database Using Flutter Firebase Firestore 如何使用 where 子句从 firebase 正确获取数据,并使用 flutter 从 firebase 中正确获取数据 - How to correctly fetch data from firebase using where clauses and custom filters from firebase using flutter 如何使用 flutter 从 firebase 中检索数据 - How to retrieve data from firebase using flutter 是否可以在使用 flutter 查询来自 firebase 的数据时使用多个 '.where()' 语句? - Is it possible to use multiple '.where()' statements in querying data from firebase using flutter? 使用 flutter 从 firebase Firestore 获取数据 - fetching data from firebase firestore using flutter Flutter Firebase - 在具有多个文档的集合中使用 where 检索数据 - Flutter Firebase - retrieving data using where in a collection with multiple docs 从 Flutter Firebase Realtime Database when onChildAdded 获取数据 - Getting data from Flutter Firebase Realtime Database when onChildAdded 如何使用 flutter 中的子值从 firebase 获取数据? - How to get data from firebase using child value in flutter? Flutter:使用 Future wait 从 Firebase 获取数据 - Flutter: Get Data from Firebase using Future wait 在 firebase 中使用 .where('uid', isEqualTo: uid) 时出现 null 错误 flutter - getting null error when using .where('uid', isEqualTo: uid) in firebase flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM