Conditional switchMap

As part a my authentication service, I’m trying to map the Streamfrom thefirebase_authpackage to a realtime listenerStream` of the corresponding user in firestore.

To achieve that, I’m using the switchMap stream transformer from the rxDart package.

However, the value returned from FirebaseAuth.instance.onAuthStateChangedcan be null and in that case I would like the resulting stream to return null as well.

Thing is, I’m not sure how to achieve that. Should I return an single value Stream of value null? If so how?

I tried returning a generator function async* {yield null}; but it didn’t work.

    _userStream = FirebaseAuth.instance.onAuthStateChanged
        .transform(SwitchMapStreamTransformer((FirebaseUser user) {
      // user can be null here what should I return ?
      return () async* {yield null}; // this doesnt work
      return FirestoreUserService.getUserStreamFromId(user.uid);
    }));

Cheers! 🙂

1 possible answer(s) on “Conditional switchMap

  1. @hoc081098 Thanks! Exactly what I needed 🙂

    For the record, here is what my function ended looking like

      @override
      Stream<UserModel> get onAuthStateChanged {
        return FirebaseAuth.instance.onAuthStateChanged.transform(
            SwitchMapStreamTransformer((user) => user == null
                ? Stream.value(null)
                : locator.get<UserService>().getUserStreamFromId(user.uid)));
      }