Created
September 16, 2023 09:20
-
-
Save wjkoh/ec089937478812d318de885f8d1c4616 to your computer and use it in GitHub Desktop.
Flutter: A chain of two streams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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