Flutter将多个FireStore流与Rxdart结合

Flutter将多个FireStore流与Rxdart结合

我认为我需要将其合并到某个位置,但是我找不到方法和位置。我要在StreamBuilder中使用吗?我带你到这里来等待您的帮助。

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

最佳答案

最后操作添加此代码

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

}

关于Flutter将多个FireStore流与Rxdart结合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59109855/

10-11 06:50