Flutter get reference after navigation pop - flutter

I would like to get a reference to the screen I am going to show.
With this code:
Navigator.of(context).pop()
I actually going back to the previous screen but I would like to access that reference to do some work.
How can I do that?
With my iOS background working in Swift I would have done something like that:
var newScreen = Navigator.of(context).pop();

You cannot create a reference because when you pop the current "route" all the data will be disposed.
But you can return a pop result to the awaiting parent as explained by official docs.
Something like:
//When you will dispose the pushed page/route you can pass your result to pop method
Navigator.pop(context, 'This is my new page result');
//In the parent page you will await for the result
final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyPushedPage()),
);
print(result) //--> This is my new page result

Related

Flutter go_router how to return result to previous page?

I'm trying to open a page and get returned result with go_router package.
In Navigation 1.0 I use this:
final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SecondRoute()),
);
// handle result
But I can't seem to do it with go_router. Any solution or explaination?
You can do this with GoRouter.of(context).addListener.
First you push your new page and add a listener afterwards
GoRouter.of(context).push("/page/${page!.id}/edit");
GoRouter.of(context).addListener(watchRouteChange);
The listener function can look something like this
watchRouteChange() {
if (!GoRouter.of(context).location.contains("/edit")) { // Here you check for some changes in your route that indicate you are no longer on the page you have pushed before
// do something
GoRouter.of(context).removeListener(watchRouteChange); // remove listener
}
}
Presently there is no way to achieve this using go_router. You can use go_router_flow which is exactly like go_router with this pop with value feature.
final bool? result = await context.push<bool>('/page2');
WidgetsBinding.instance.addPostFrameCallback((_) {
if(result){
print('Page returned $result');
}
});
You can use the callback by putting the function in extra object when push new screen.
Example
Screen A -push-> Screen B ->pop with result -> Screen A (get results)
Define the function type to put
typedef AddNewEventResult = void Function(Result result);
Push A -> B
GoRoute(
path: kScreenB,
builder: (BuildContext context, GoRouterState state) => ScreenB(addNewEventResultstate.extra! as AddNewEventResult),
)
When screen B has done, just pop from B to A and attached the result (Result)
Navigator.of(context).pop();
widget.addNewEventResult(true, Result());
This flow is described in the docs here: https://gorouter.dev/user-input
Generally you have to update the data and return some value back as a route with params and the screen itself should manage updates / data manipulation.
I don't want to copy paste their code here, but the answer you are looking for is in the docs page above.
Updated with link from archive: https://web.archive.org/web/20220325235726/https://gorouter.dev/user-input
Thanks ahmetakil
When you are done with the next screen just use Navigator.pop(context,true); and true is something that you want to send to the previous screen. You can send anything I'm just using true for reference. This will allow your result variable to get data and perform anything.

How do we pass or propagate data backwards through the Navigation stack in Flutter?

1.FlatButton(
onPressed: () async {
var typedName=await Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return CityScreen();
},
),
);
print(typedName); //prints the value of cityName
}
2. FlatButton(
onPressed: () {
Navigator.pop(context, cityName);
},
The no.1 is coming from file loading_screen and no.2 is coming from city_screen. Can you anyone help me understand what is happening that when you pass a variable or anything in the pop? And when come that onPress method is still working because the the method Navigator.push has been already assigned to the variable but still that method Navigator.push is working when I pressed the button?Does that onPress doesn't care about the variable TypedName and just looks for the method Navigator.push?
If you use push method it will create the new screen over your current page and pop will remove last screen, you cannot pass name with both, you can check basics of navigation here, if you want to pass name to next page you should use something like pushnamed method and you can see the logic of passing name here. And for all methods you should check here.

How to call a function to update a value after popping a screen in Flutter?

Screen 1: shows list of item with add button.
Screen 2: form to add a new item to the list.
Screen 2 >> Screen 1 - While calling navigator.pop() in screen 2, how to call method/setState (to update list) in screen 1?
Can anyone help me out?
I don't want to relaunch screen again. I just need to run a method to update my list after popping previous screen?
When you navigate from Screen 1 to Screen 2, you use the push method. The push method returns a Future object.
You can use the then method of Future to execute your code after Screen 2 was popped from the navigation stack.
Navigator.of(context)
.push(MaterialPageRoute(
builder: (context) => Screen2(),
))
.then((value) {
// you can do what you need here
// setState etc.
});
You can use Provider or BLoc or you can await for the result when you push the page
You can do it like this :
// this method waits for the pop to be called
var data = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => LoginScreen()),
);
debugPrint(data);
// here the second argument is the data
Navigator.pop(context, "data"); // popped from LoginScreen().
output
data
Same method can also be done like below
// this method waits for the pop to be called
Navigator.push(
context,
MaterialPageRoute(builder: (context) => LoginScreen()),
).then((data){
// then will return value when the loginScreen's pop is called.
debugPrint(data);
});
Here is a good article to look into this
https://medium.com/flutter-community/flutter-push-pop-push-1bb718b13c31
Navigator.pop has a second argument called result.
You could then return the value that you need to the first page that will read it as the return value of Navigator.push

In Flutter Which method gets called when pop back to Screen A from Screen B

I have updated my Sqlite Table in Screen B and want to reflect that change in Screen A Also.
So when Pop back from Screen B> Screen A nothing updated.
In Android, we have onResume() to make changes reflect in Activity A.
I have used Widgets Binding Observer but it works if app background, foreground.
Navigator.of(context).pop();
You can await Navigator.push, when pop is called anything below Navigator.push will be called. Ex:
await Navigator.push(SomePage());
/// This will be called after SomePage cals Navigator.pop();
doSomething();
You can get back from the second page with data like this:
// Second Page
Navigator.pop(context, 'result');
Then by the below code on the first page, you are able to lunch the second page and wait until return from it and get data.
// First Page
var result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondPage()),
);

How to refresh a page after back button pressed

I have a "list page". and an add page. User clicks "add page", goes to a new page where they add some entity. Then I pop the Navigator, and user goes back to "list page".
At this point, I want to get notified somehow - callback maybe? override a method, I am not really sure - and re-read the information from database.
Is there a hook like this in Flutter? Or is there another pattern that I should use?
The Navigator.push method returns a future of the generic type of the Route:
Navigator.of(context)
.push(new MaterialPageRoute<String>(...))
.then((String value) {
print(value);
});
And inside the new route, when you have the value you need:
Navigator.of(context).pop('String');
If at your list page, you create function let say retrieveData() to read list of the data then call it inside initState, if you're using pushNamed route, the simple solution would be
Navigator.pushNamed(context, '/adddatapage').whenComplete(retrieveData());
If you're using push route,
Navigator.of(context).push(new MaterialPageRoute(builder: (context) => AddPage())).whenComplete(retrieveData);
That should be enough. No need to additional code on the add page. This will be works if user press back button also, again, without additional code in the add page.
Adding a option to #ian 's answer
Navigator.of(context)
.push(new MaterialPageRoute<String>(...))
.then((value) => setState(() => {}));
This worked for me. This will rebuild whenever page is loaded.
Write below code when redirect to screen 2,
Navigator.pushNamed(context, '/screen2').then((_) {
// This block runs when you have returned back from screen 2.
setState(() {
// code here to refresh data
});
});
Use
Navigator.pop(context);
in screen 2 to back to screen 1
i use Navigator.pushAndRemoveUntil, it will refresh the page and load the data again, this is example :
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) =>
CustomerMainPage()),
(Route<dynamic> route) =>
false);
I passed a callback method into the "add" page as a Navigator route argument. The callback is a method in my list page. The "add" page executes the callback when exiting. The callback method then simply refreshes the data and calls setState to refresh the screen.
My architecture is using Provider and viewmodels, but it should be straight-forward in a standard app as well.
For named route:
Navigator.pushNamed(context, '/page2').then((_) {
// This block runs when you have returned back to the 1st Page from 2nd.
setState(() {
// Call setState to refresh the page.
});
});
For unnamed route:
Navigator.push(context, MaterialPageRoute(builder: (_) => Page2())).then((_) {
// This block runs when you have come back to the 1st Page from 2nd.
setState(() {
// Call setState to refresh the page.
});
});