dart Flutter多重火灾查询

iezvtpos  于 2023-01-18  发布在  Flutter
关注(0)|答案(3)|浏览(116)

我尝试对Firestore进行多个查询,并将结果合并到一个流(如here)中。我尝试使用StreamGroup.merge(),但它只返回一个流的结果。我注意到它确实获取了所有流的数据,但只在所有操作完成时返回一个流。以下是我所做的:

Stream getStream(){
    List<Stream> streams = [];     

    streams.add(Firestore.instance.collection(Constants.REQUESTS_NODE).
    where("municipality",isEqualTo: "City of Johannesburg Metropolitan").
    where("service_id",isEqualTo: 2).
    snapshots());
    streams.add(Firestore.instance.collection(Constants.REQUESTS_NODE).
    where("municipality",isEqualTo: "Lesedi Local").where("service_id",isEqualTo: 2).
    snapshots());    

    return StreamGroup.merge(streams);

  }

我错过了什么,做错了什么?我这样做的原因是为了弥补Firestore缺少OR操作符。

6vl6ewon

6vl6ewon1#

我在网上找到了这个,希望能有所帮助:

`import 'package:async/async.dart';

Stream<DocumentSnapshot> stream1 = firestore.document('/path1').snapshots();
Stream<DocumentSnapshot> stream2 = firestore.document('/path2').snapshots();
StreamZip bothStreams = StreamZip([stream1, stream2]);

// To use stream
bothStreams.listen((snaps) {
 DocumentSnapshot snapshot1 = snaps[0];
 DocumentSnapshot snapshot2 = snaps[1];

 // ...
});`
5n0oy7gb

5n0oy7gb2#

你可以使用whereIn条件,它是一个List<dynamic>,这是我的工作!
例如:

Firestore db = Firestore.instance;
db.collection
.where("status", whereIn: ["available", "unavailable", "busy"])
.getDocuments();
k75qkfdt

k75qkfdt3#

使用collectionGroup()

集合组由具有相同ID的所有集合组成。默认情况下,查询从数据库中的单个集合检索结果。使用集合组查询可以从集合组而不是单个集合检索文档。
来自文档的示例
例如,您可以通过向每个城市添加地标子集合来创建地标集合组:

final citiesRef = FirebaseFirestore.instance.collection("cities");

final ggbData = {"name": "Golden Gate Bridge", "type": "bridge"};
citiesRef.doc("SF").collection("landmarks").add(ggbData);

final lohData = {"name": "Legion of Honor", "type": "museum"};
citiesRef.doc("SF").collection("landmarks").add(lohData);

final gpData = {"name": "Griffth Park", "type": "park"};
citiesRef.doc("LA").collection("landmarks").add(gpData);

final tgData = {"name": "The Getty", "type": "museum"};
citiesRef.doc("LA").collection("landmarks").add(tgData);

final lmData = {"name": "Lincoln Memorial", "type": "memorial"};
citiesRef.doc("DC").collection("landmarks").add(lmData);

final nasaData = {
  "name": "National Air and Space Museum",
  "type": "museum"
};
citiesRef.doc("DC").collection("landmarks").add(nasaData);

final upData = {"name": "Ueno Park", "type": "park"};
citiesRef.doc("TOK").collection("landmarks").add(upData);

final nmData = {
  "name": "National Musuem of Nature and Science",
  "type": "museum"
};
citiesRef.doc("TOK").collection("landmarks").add(nmData);

final jpData = {"name": "Jingshan Park", "type": "park"};
citiesRef.doc("BJ").collection("landmarks").add(jpData);

final baoData = {"name": "Beijing Ancient Observatory", "type": "musuem"};
citiesRef.doc("BJ").collection("landmarks").add(baoData);

我们可以使用前面描述的简单查询和复合查询来查询单个城市的地标子集合,但是您可能还希望同时检索每个城市的地标子集合的结果。
地标集合组由具有ID地标的所有集合组成,您可以使用集合组查询对其进行查询。例如,以下集合组查询将检索所有城市的所有博物馆地标:

FirebaseFirestore.instance
    .collectionGroup("landmarks")
    .where("type", isEqualTo: "museum")
    .get()
    .then(
      (res) => print("Successfully completed"),
      onError: (e) => print("Error completing: $e"),
    );

相关问题