如何根据第一个流中的事件初始化第二个流?

How to init a second stream depending on events from the first stream?

在我的 BLOC 中,我需要收听 FirebaseAuth.instance.onAuthStateChanged 并且根据用户 uid 将初始化第二个流 Firestore.instance.collection('accounts/${uid}/private').snapshots() 并将结果合并到一个模型中:

    class MyPageModel {
      bool userSignedIn;
      List<String> privateData;
    }

这个模型需要用BehaviorSubject流出。使用 rxdart 完成此任务的最佳方法是什么?

检查下面的代码,了解如何组合两个条件流:

class TheBLoC{
  BehaviorSubject<MyPageModel> _userDataSubject = BehaviorSubject<MyPageModel>();
  // use this in your StreamBuilder widget
  Stream<MyPageModel> get userData => _userDataSubject.stream;
  // a reference to the stream of the user's private data
  StreamSubscription<QuerySnapshot> _subscription;
  // bool with the state of the user so we make sure we don't show any data 
  // unless the user is currently loggedin.
  bool isUserLoggedIn;

  TheBLoC() {
    isUserLoggedIn = false;
    FirebaseAuth.instance.onAuthStateChanged.listen((firebaseUser) {
      if (firebaseUser.isAnonymous()) {
        isUserLoggedIn = false;
        final event = MyPageModel();
        event.isSignedIn = false;
        _userDataSubject.add(event);
        // cancel the previous _subscription if available
        _subscription?.cancel();
        // should also probably nullify the _subscription reference 
      } else {
        isUserLoggedIn = true;
        // the user is logged in so acces his's data
        _subscription = Firestore.instance.collection
          ('accounts/${firebaseUser.uid}/private')
            .snapshots().listen((querySnapshots){              
              if(!isUserLoggedIn) return;
              final event = MyPageModel();              
              event.isSignedIn = true;
              // use the querySnapshots to initialize the privateData in 
              // MyPageModel
              _userDataSubject.add(event);
        });
      }
    });
  }

}