Skip to content

Instantly share code, notes, and snippets.

@wjkoh
Created September 16, 2023 09:20
Show Gist options
  • Select an option

  • Save wjkoh/ec089937478812d318de885f8d1c4616 to your computer and use it in GitHub Desktop.

Select an option

Save wjkoh/ec089937478812d318de885f8d1c4616 to your computer and use it in GitHub Desktop.
Flutter: A chain of two streams
class ProfileModel extends ChangeNotifier {
final Stream<User?> _userStream = FirebaseAuth.instance.authStateChanges();
User? user;
Stream<DocumentSnapshot<Profile>>? _profileStream;
Profile? profile;
ProfileModel() {
_userStream.listen((User? newUser) {
user = newUser;
profile = null;
if (user != null) {
_profileStream = FirebaseFirestore.instance
.collection('profiles')
.withConverter(
fromFirestore: Profile.fromFirestore,
toFirestore: (Profile profile, options) => profile.toFirestore(),
)
.doc(user!.uid)
.snapshots();
_profileStream!.listen((event) {
profile = event.data();
notifyListeners();
});
}
notifyListeners();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment