mac*_*mac 5 dart flutter google-cloud-firestore
我正在尝试向我的 flutter firebase 查询添加一个OR条件。
这是我的代码,但我想检查哪里receiver_name等于kimORsender_name等于kim
StreamBuilder(
stream: Firestore.instance.collection("payments").where("receiver_name", isEqualTo: userActive).snapshots(),
builder: (context, snapshot){
return Container ( child:ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data.documents.length,
padding: EdgeInsets.all(0),
controller: ScrollController(keepScrollOffset: false),
itemBuilder: (context, index){
DocumentSnapshot documentSnapshot = snapshot.data.documents[index];
Run Code Online (Sandbox Code Playgroud)
OR 运算符现已在 Firebase 中可用。
现在可以通过使用来实现Filter.or。请点击此链接了解更多详细信息:OR 运算符
为此,您需要 Cloud Firestore 版本 24.4.5
在颤振中你需要cloud_firestore: ^4.5.0
要查询它,你可以这样写:
Firestore.instance.collection("payments")
.where(Filter.or(
Filter("receiver_name", isEqualTo: "kim"),
Filter("sender_name", isEqualTo: "kim")
)).snapshots();
Run Code Online (Sandbox Code Playgroud)
更新:自 2023 年 3 月起,Firestore 上可以使用跨多个字段的 OR 条件,如OR有关查询的新文档中所示。请务必查看该文档,包括对使用 OR 子句的查询的限制。
OR 条件的语法示例:
snapshot = await db.collection('/76015089').where(
Filter.or(
Filter("name", isEqualTo: "John"),
Filter("id", isNotEqualTo: "null"),
)
).get();
print('Got ${snapshot.docs.length} documents');
for (var doc in snapshot.docs) {
print(' ${doc.id}: name=${doc.get("name")} id=${doc.get("id")}');
}
Run Code Online (Sandbox Code Playgroud)
对于这个特定的用例,您还可以考虑以下替代解决方案:
如果您participants向文档添加一个数组字段,其中包含发送者和接收者的 UID 字段,则您可以使用查询array-contains来获取用户作为发送者或接收者的所有付款。
要将 UID 添加到此类数组,请务必使用array-union运算符:
documentRef.updateData({"participants": FieldValue.arrayUnion("uidOfUser")});
Run Code Online (Sandbox Code Playgroud)
然后要查询它,您可以使用:
collectionRef.where('participants', arrayContains: "uidOfUser")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3820 次 |
| 最近记录: |