Flutter how to execute when clicking back button


I have a page that is called from bottom tab nav which executes a initState function, I then navigate to a page via button click that has details and actions to take, however when I click the back button to goto originating page, the initState does not run again, I have found out that because Flutter does not destroy the page when you put one on top, since the NAV is not on new page as its not in the menu, how do I make initState run again on clicking back button or is there another way to listen for that navigate from back button and run the code that needs to update data?

Any help would be great.


You can override the default back arrow on the AppBarand then specify the value you would like to return to trigger the change of the state when Navigator.pop is called:


so you need to have something like this in your onPressed callback of your navigation button

onPressed: ()async{
            var nav = await Navigator.of(context).push(newRoute);
              //change the state

and in your newRoute you should have something like this

new AppBar(
        leading: new IconButton(
          icon: new Icon(Icons.arrow_back),
          onPressed: (){Navigator.pop(context,true)}

I am checking for both values null or true because the null value is returned when the user hits the BackButton on android screen (the one in the bottom of the screen). I also believe the null will also be returned with the default BackButton in Flutter so you do not actually need to override the leading property, but I have not checked that myself, so it may be worth checking.

Answered By – Shady Aziza

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *