I am trying to implement that when a user clicked a back button on the native screen then I need to call a function.
For Example:
I created a chat app in which on pressing the start chat button user is redirected to the native screen and whenever the user pressed the back button on the native screen and is redirected to the flutter screen that time I need to call a specific function.
In this on pressing start chat user is redirected to the native screen and chat with the agent.
Then pressing the back button on the native screen the user is redirected to the flutter screen so I need to call a specific function whenever the user is redirected to the flutter screen. Please help me as soon as possible stack community.
CodePudding user response:
you can use WillPopScope() and this video may help u
Flutter Tutorial - Handle Back Button Pressed [2021] WillPopScope Widget
CodePudding user response:
Think you go from Screen 1 to Screen 2. You use the following code:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Screen2()),
)
So, when user presses the back button on Screen 2 and comes back to Screen one you want to reload api data, right? Just do the following in the above function:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Screen2()),
).then((value) {
callApiToReloadData();
});
So whenever user returns to Screen 1 from Screen 2 using Navigation.pop() or by pressing the appbar back button, callApiToReloadData() function will be called.


