【问题标题】:Flutter Combine Multiple FireStore Streams With RxdartFlutter 将多个 FireStore 流与 Rxdart 相结合
【发布时间】:2020-03-25 08:24:28
【问题描述】:

我想我需要在某个地方将它们合并在一起,但我不知道如何以及在哪里。我想在 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));
      });
    });
  }

【问题讨论】:

    标签: flutter dart stream observable rxdart


    【解决方案1】:

    添加此代码最后一次操作

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

    }

    【讨论】:

      【解决方案2】:

      如果您合并流,StreamBuilder 将仅显示来自合并流的 LAST 事件。我相信你可以这样做:

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

      如果有帮助请告诉我

      【讨论】:

        猜你喜欢
        • 2019-02-13
        • 2019-07-23
        • 2020-11-01
        • 2019-04-16
        • 2020-02-28
        • 2021-06-05
        • 1970-01-01
        • 2012-12-31
        • 2018-05-14
        相关资源
        最近更新 更多