I have a simple flow:
a
UserModel
implementingChangeProvider
which wraps the state of the user (if it is logged in and utilities to log him in/out). In particular logout looks like:void logout() { user = null; notifyListeners(); }
a
UserPage
widget with (among others):class UserPage extends StatelessWidget { @override Widget build(BuildContext context) { // ... adding only relevant code // a text with the user first letter of the email Text(context.watch<UserModel>().user.email[0]) // a logout button with the following onPressed method TextButton( \\ ... onPressed: () { context.read<UserModel>().logout(); Navigator.pop(context); } ) } }
I was expecting that pressing logout and popping the UserPage
widget will not let flutter rebuild it. However it is not the case and the notifyListeners()
in logout
method makes flutter rebuild the widget and trigger a NullPointerException
(since the user is null and the email can't be accessed).
I could deal with it (checking if the user object is != null but I would like to understand why this happens).
Is it correct to assume pop
destroys the widget? If not, how should I handle this case?
When a user is logged out I don't want to have in memory this widget nor deal with its existence. I would expect to create a UserPage
when a user logs in and destroy it after its logout