如何将 rxdart Observable 转换为 Stream

how to convert rxdart Observable to Stream

我在 git 查看代码 firebase auth and it works fine, but a problem came when, I upgrade dependencies, particularly for rxdart from ^0.21.0 to current (^0.24.1), I see other 引用

The Observable can be replaced by Stream in accepted answer. I am able to change

Observable<FirebaseUser> user; // firebase user
..
user = Observable(_auth.onAuthStateChanged);

Stream<FirebaseUser> user; // firebase user
..
user = _auth.onAuthStateChanged;

但是

Observable<Map<String, dynamic>> profile; // custom user data in Firestore
..
profile = user.switchMap((FirebaseUser u) {
      if (u != null) {
        return _db
            .collection('users')
            .document(u.uid)
            .snapshots()
            .map((snap) => snap.data);
      } else {
        return Observable.just({});
      }
    });

我试过了

Object profile;
..
user.map( (FirebaseUser u) {
      if (u != null) {
        return _db
            .collection('users')
            .document(u.uid)
            .snapshots()
            .map((snap) => snap.data);
      } else {
        return Stream.empty();
      }
    });

但它没有触发监听,也没有读取用户配置文件。

你可以这样使用它

Stream<Map<String, dynamic>> profile = user.switchMap((u) {
      if (u != null) {
        return _db
            .collection('users')
            .document(u.uid)
            .snapshots()
            .map((snap) => snap.data);
      } else {
        return Stream.value({});
      }
    });