Flutter/Dart QuerySnapshot isEqualTo or operator?

一笑奈何 提交于 2020-08-09 12:22:51

问题


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.

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;  
 }

回答1:


Cloud Firestore does not support the following types of queries:

  • Logical OR queries. In this case, you should create a separate query for each OR condition and merge the query results in your app.

Source: Query limitations

Edit:

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,
  ];
}


来源:https://stackoverflow.com/questions/58191550/flutter-dart-querysnapshot-isequalto-or-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!