How to properly navigate to previous page with getx? - flutter

I have a pages
A>B>C>D>E
I want to navigate back from page E>B>A
I tried with
Get.offNamedUntil('B', (route) => false);
But it worked like E>B and then doesn't redirect back to A. Is there any other way to do so?

I would say that you forget a / in the named route, and as a recommendation, don't return false in the callback, set a condition to stop if the named route was not found in the routes stack, as example use route.isFirst :
Get.offNamedUntil('/B', (Route<dynamic> route) => route.isFirst);

Related

Why Pages can match the Navigator.push on flutter?

when I use navigator2.0 pages to control route.
then when I use Navigator.push( context, MaterialPageRoute(builder: (_) => DemoPage()));
that's works ok.
but I did not change the pages, I also find pages don't changed.
Why Pages can match the Navigator.push?
for example: pages only have one,then Navigator.push(),there are actually two routes ,but page.length has always been 1.
running effect is ok, pop can also work ok,everything looks ok, how is this done?
Thank you very much for your reply。

how to switch to a new root in flutter without saving the old one in the stack?

based on the documentation :
To switch to a new route, use the Navigator.push() method. The push()
method adds a Route to the stack of routes managed by the Navigator.
but this will cause an issue as if I click the back button it will get me back to the first screen root , so how I prevent that ? when i change the root i don't want the old one to be saved in the root stack
Navigator.push(
context, MaterialPageRoute(builder: (context) => HomeScreen()));
Use
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => HomeScreen()))
From the docs we see this method will
Replace the current route of the navigator by pushing the given route and then disposing the previous route once the new route has finished animating in.

Getting back to the first page in the stack in Flutter

In my Flutter app I want to get back to the home page by removing everything but the first page from the stack.
I've found two ways of doing it and they both seem to work but since I'm fairly new to Flutter I was wondering of one of these methods is best :-
Navigator.of(context).popUntil((route) => route.isFirst);
Navigator.pushNamedAndRemoveUntil(context, '/', (_) => false);
You can use both of them.
But, in my own opinion (as I read the docs), in order to use the pushNamedAndRemoveUntil method, you need to name your routes.
On the other hand, If you do not use named routes, you can use the popUntil method. Because it's not using named routes and much simpler.
The answer is pushAndRemoveUntil. There is a method like this available for you, if you don't want to use named route in your flutter app. Read about it here.
So, now what you can do it:
// It accepts Route object, so we can use Material Page Route
// We name your first page as HomePage, so don't get Confused
Navigator.pushAndRemoveUntil(context,
MaterialPageRoute(builder: (context) => HomePage()),
(_) => false
);
Read about MaterialPageRoute class, and add it to your bucket, because it is a very useful thing, if you don't know about it already.

Flutter logout to remove all routes

Below is my app widget tree. If user is not logged in then login page is shown if user is logged in then work page. on account page I have logout button.Logout button implements push replacement and shows login page.
Issue is - When user click on back button again work page is shown.
How to remove all the routes from widget tree and only show login page after logout action?
Note - I am not using Named routes, just Push , Pop and Replacement
If you are using namedRoutes, you can do this by simply :
Navigator.pushNamedAndRemoveUntil(context, "/login", (Route<dynamic> route) => false);
Where "/login" is the route you want to push on the route stack.
Note That :
This statement removes all the routes in the stack and makes the pushed one the root.
Close your dialog by calling,
Navigator.pop(context);
and then call pushReplacement
void _doOpenPage() {
navigator.pushReplacement(
MaterialPageRoute(builder: (BuildContext context) => MyHomePage()));
}
pushReplacement Replace the current route of the navigator by pushing the given route and then disposing the previous route once the new route has finished animating in.
Read More here

How to refresh the page when user comes back to it from any other page using back key?

I have a page which is calling multiple classes and different widgets
and by clicking on it user can hop to any page.
Currently the issue is I want to refresh the original page everytime it is shown to user.
When it is freshly loaded (causing no issue for now).
When user comes back to it from some other page using back key.
old data is shown on the page, which I am trying to get rid of.
I have tried using Navigator.push method (Observer class method) and tried to listen when user presses backkey.
but there are multiple pages serving unique requests and I don't want to link everyone with that first page.
as while clicking Navigator.pop(context) method i'll have to pass some string.
Does any one know
how can I refresh the page when users comes back to it using backkey.
Navigator.push returns a Future when the MaterialPageRoute passed into it is resolved. You could await that Future to know when some other view has popped back to it.
Example,
Navigator.of(context).push(MaterialPageRoute(
builder:
(context) => NewView(),
),
).then((_) {
// Call setState() here or handle this appropriately
});
Had the same issue, the following worked for me (below code is in FirstPage()):
Navigator.push( context, MaterialPageRoute( builder: (context) => SecondPage()), ).then((value) => setState(() {}));
After you pop back from SecondPage() to FirstPage() the "then" statement will run and refresh the page.
NavigatorObserver can listen to route changes.
You need to create your implementation overriding didPop, didPush etc.
Then you can pass it to navigatorObservers field of MaterialApp or Navigator