Namus Keyfe
Namus Keyfe

Reputation: 87

Flutter Combine Multiple FireStore Streams With Rxdart

I think I need to merge it together somewhere, but I can't find out how and where. I want to use in StreamBuilder ? I brought you up here waiting for your help..

getCombinedMatches(uid) {
    return Firestore.instance
        .collection('matches')
        .where('match', arrayContains: uid)
        .snapshots()
        .map((convert) {
      return convert.documents.map((f) {
        Observable<Matches> match = f.reference
            .snapshots()
            .map<Matches>((document) => Matches.fromMap(document.data));
        Observable<User> user = Firestore.instance
            .collection("users")
            .document(uid)
            .snapshots()
            .map<User>((document) => User.fromMap(document.data));

        Observable<Message> message = Firestore.instance
            .collection('matches')
            .document(f.documentID)
            .collection("chat")
            .orderBy('dater', descending: true)
            .limit(1)
            .snapshots()
            .expand((snapShot) => snapShot.documents)
            .map<Message>((document) => Message.fromMap(document.data));

        return Observable.combineLatest3(match, user, message,
            (matches, user, message) => CombinedStream(matches, user, message));
      });
    });
  }

Upvotes: 5

Views: 1195

Answers (2)

Cenk YAGMUR
Cenk YAGMUR

Reputation: 3451

Add this code last operation

getCombinedMatches(uid) {
return Observable(Firestore.instance
    .collection('matches')
    .where('match', arrayContains: uid)
    .snapshots())
    .map((convert) {
  return convert.documents.map((f) {
    Stream<Matches> match = f.reference
        .snapshots()
        .map<Matches>((document) => Matches.fromMap(document.data));
    Stream<User> user = Firestore.instance
        .collection("users")
        .document(uid)
        .snapshots()
        .map<User>((document) => User.fromMap(document.data));

    Stream<Message> message = Firestore.instance
        .collection('matches')
        .document(f.documentID)
        .collection("chat")
        .orderBy('dater', descending: true)
        .limit(1)
        .snapshots()
        .expand((snapShot) => snapShot.documents)
        .map<Message>((document) => Message.fromMap(document.data));

    return Observable.combineLatest3(match, user, message,
        (matches, user, message) => CombinedStream(matches, user, message));
  });
}).switchMap((observables) {
      return observables.length > 0
          ? Observable.combineLatestList(observables)
          : Observable.just([]);
    });

}

Upvotes: 3

alexandreohara
alexandreohara

Reputation: 264

If you merge streams, the StreamBuilder will only display the LAST event from the merged stream. I believe you can do something like this:

// snapshotList will be your AsyncSnapshot. snapshotList.data will be your List<QuerySnapshot>

StreamBuilder<List<QuerySnapshot>>(
    stream: streamList,
    builder: (BuildContext context, AsyncSnapshot<List<QuerySnapshot>> snapshotList) {
        snapshotList.data.forEach((document) {
            return something
        }
    }
)

Let me know if that helps

Upvotes: 0

Related Questions