Flutter/Dart QuerySnapshot isEqualTo或运算符? [英] Flutter/Dart QuerySnapshot isEqualTo or operator?

查看:46
本文介绍了Flutter/Dart QuerySnapshot isEqualTo或运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下集合中是否有一个文档,其"test"字段等于1 2或4.但是很遗憾,我没有找到合适的解决方案.

I would like to ask if there is a document in the collection with the field "test" equal to 1 2 or 4. But unfortunately I have not found a suitable solution.

这是我的方法不幸的是,这在这里不起作用(等于1 | 2 | 4).是否还是运算符? (|)

This is my approach Unfortunately, this does not work here (is Equal To 1 | 2 | 4). Is that no or operator? (|)

Future getPostsToday() async { 
   ...
    var firestore =  Firestore.instance;
    QuerySnapshot qn = await firestore.collection("Data").where("test",   isEqualTo: 1 | 2 | 4 ).getDocuments();
    return qn.documents;  
 }

推荐答案

Cloud Firestore不支持以下类型的查询:

Cloud Firestore does not support the following types of queries:

  • 逻辑OR查询.在这种情况下,您应该为每个OR条件创建一个单独的查询,并将查询结果合并到您的 应用程序.
  • Logical OR queries. In this case, you should create a separate query for each OR condition and merge the query results in your app.

来源:查询限制

所以我认为您可以执行以下操作:

So I think you could do something like this:

Future<List<DocumentSnapshot>> getPostsToday() async {
  final collection = Firestore.instance.collection("Data");

  // separate query for each OR condition
  final querySnapshots = await Future.wait([
    collection.where("test", isEqualTo: 1).getDocuments(),
    collection.where("test", isEqualTo: 2).getDocuments(),
    collection.where("test", isEqualTo: 4).getDocuments(),
  ]);

  // merge the query results and return
  return <DocumentSnapshot>[
    ...querySnapshots[0].documents,
    ...querySnapshots[1].documents,
    ...querySnapshots[2].documents,
  ];
}

这篇关于Flutter/Dart QuerySnapshot isEqualTo或运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆