Flutter 多个 Firestore 查询

spo*_*oss 6 dart firebase flutter google-cloud-firestore

我正在尝试对 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);

  }
Run Code Online (Sandbox Code Playgroud)

我错过了什么,做错了什么?我这样做的原因是为了弥补 Firestore 缺乏OR运营商的问题。

Moh*_*med 7

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

`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];

 // ...
});`
Run Code Online (Sandbox Code Playgroud)


dev*_*996 5

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

例如:

Firestore db = Firestore.instance;
db.collection
.where("status", whereIn: ["available", "unavailable", "busy"])
.getDocuments();
Run Code Online (Sandbox Code Playgroud)