Flutter Combine Multiple FireStore Streams With Rxdart. Use the following example code to combine Multiple FireStore Streams With Rxdart.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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([]); }); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.