Following this blog post: https://blog.ishangavidusha.com/flutter-authentication-flow-with-go-router-and-provider
I have implemented a flutter app using the go router. I have added the firebase authentication handler in the initState()
of my flutter App:
@override
void initState() {
appService = AppService(widget.sharedPreferences);
authService = AuthService();
authSubscription = authService.onAuthStateChange.listen(onAuthStateChange);
super.initState();
}
Here in my authservice class, I have a constructor like this:
AuthService() {
authSubscription =
FirebaseAuth.instance.authStateChanges().listen((User? user) {
if (user == null) {
//appService.loginState = false;
print('User is currently signed out!');
_onAuthStateChange.add(false);
} else {
//appService.loginState = true;
print('User is signed in!');
_onAuthStateChange.add(true);
}
});
}
Everything works fine. When I change something in my app and save the changes, the app performs a hot reload. It seems like the hot reload keeps the previous event handler active as after each hot reload, I have one more call to my authStateChanges
event handler when I perform a login task. After a hot reload I would have two print statement print('User is signed in!');
after a login. And one more after each hot reload.
Is this acceptable behavior for development or is my app not architected correctly?