yokozuna
yokozuna

Reputation: 23

Merge Firestore streams using rxDart

I am trying to merge two streams from Firestore into one stream using RxDart, but it's only returning results of one stream

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

Upvotes: 2

Views: 2534

Answers (2)

Maya Mohite
Maya Mohite

Reputation: 683

You can use mergeWith to get one Stream from two seperate streams using RxDart like below,

 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 stream1.mergeWith([stream2]);
  }

Upvotes: 4

dumazy
dumazy

Reputation: 14435

Depending on your use case, you might not need RxDart to do this. If you just want to have two Firestore streams merged to one Dart Stream you can use StreamZip from the dart:async package.

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

Upvotes: 9

Related Questions