使用rxDart合并Firestore流


问题内容

我正在尝试使用RxDart将Firestore中的两个流合并为一个流,但是它仅返回一个流的结果

Stream getData() {
    Stream stream1 = Firestore.instance.collection('test').where('type', isEqualTo: 'type1').snapshots();
    Stream stream2 = Firestore.instance.collection('test').where('type', isEqualTo: 'type2').snapshots();
    return Observable.merge(([stream2, stream1]));
}

问题答案:

根据您的用例,您可能不需要RxDart来执行此操作。如果您只想将两个Firestore流合并到一个Dart中Stream,则可以StreamZipdart:async包中使用。

import 'dart:async';

Stream<List<QuerySnapshot>> getData() {
  Stream stream1 = Firestore.instance.collection('test').where('type', isEqualTo: 'type1').snapshots();
  Stream stream2 = Firestore.instance.collection('test').where('type', isEqualTo: 'type2').snapshots();
  return StreamZip([stream1, stream2]).asBroadcastStream();
}