Reputation: 1212
I would like to execute some code whenever a page in my app is left.
I would prefer to use a general solution rather than managing the cases where the user can go back. I've tried to use the dispose()
and deactivate()
calls but they don't work as expected (I've added some debug messages but they don't appear when I leave the page).
Thanks in advance
Upvotes: 2
Views: 2241
Reputation: 2252
WillPopScope is a widget that can execute your code when the page is going to pop from the navigation stack (i.e. when user goes back to the previous page whether using soft back button or physical back button).
Wrap your page inside a WillPopScope widget, and put the code that you want to execute when the user is leaving the page in onWillPop callback.
Upvotes: 6