In my app I have main_page screen and login screen, user can push to the login screen and pop, but when logging in I want to clear all routes in Navigator history before store the uid and pushRemoveUntil to main_page screen.
If i don't clear Navigator routes history, the main_page, that works with streams depending if the user is logged or not, reminds in second plane and gives an exception.
¿How can I clear all Navigator routes history pressing a button?
To do a cleaning of your navigation stack and also navigate to a specific page, try something like this:
Navigator.pushNamedAndRemoveUntil(context, "/newPage", (r) => false);
Related
After a user successfully logged in in my Flutter web app, it should be taken to a dashboard view.
The user then should not be able to navigate back to the login screen by pressing the browser back button.
I tried a lot of things, replacing the route, popping the route and then pushing the next route, all did not work.
Also using WillPopScope in the dashboard page does not work. Pressing the browser back button does not fire onWillPop.
Found some similar questions, but they all end up suggesting WillPopScope or replacing the route, both did not work for me.
Any idea what I could do?
Seems like you can't intercept the browser back buttons.
I solved the problem by using a RouteGuard of the auto_route package which intercepts the navigation and checks if the user is authenticated or not. If the user is already authenticated, the navigation to the Signin page is canceled.
Check the auto_route package for examples and further explanation: https://pub.dev/packages/auto_route#route-guards
Wrap it inside a WillPopScope and return false
WillPopScope(
onWillPop: () async => Future.value(false),
child: DashboardView(),
),
I am a beginner level Flutter developer trying to resolve the issue with the Navigation stack.
In the existing app:
When user logs in to the app, login api call is shooted
Homescreen is rendered with Navigator.popUntill method
All the APIs in the providers get shooted and the respective tabs/screens get filled with api data.
Now, I want to include Onboarding screens before Homescreen, so I made below changes:
Navigator.push method used to include Onboarding screens.
After onboarding screens are over, user clicks on "To app" button
On click of "To app" button, the login call is made again
User is redirected to Homescreen with Navigator.popUntill method.
But now the data from providers is lost as the respective screens do not show any data. I am pretty sure that something is going wrong with the Navigation stack when I use Navigator.push method to add Onboarding screens but I do not have much expertise in Flutter to debug the issue. Need help with restoring the provider data when navigating to Onboarding screens.
I think that the cause isn't in a navigation stack. Probably you placed Provider widget lower than navigation (MaterialApp).
Try something like that or show your code for better understanding.
Provider(
create: (context) => ClassThatCallsAPI(),
child: MaterialApp(
// Navigator placed here and any route will
// get the same data from provider
// and also can put some
I want to take confirmation from user before leaving the app, ideally when clicked on default system back navigation button and at time my navigation stack is empty.
Suppose I am on my homepage after login, and when I try to go back instead of returning me to login page it should show a pop up dialog. And based on the selection either close the app or leave it open.
wrap your home screen widget with WillPopScope widget and inside onWillPop callback write show your dialog and take action based on user's choice
Is there anyway to check route name of last screen after pop? When application start and land on home screen, there are several widgets like view profile, product carousel and so on.
Scenario: User navigate into product listing page, then detail page, click purchase and perform actions. After user purchased, shows purchased successful screen, call Navigator.of(context).popUntil(routeName) back to home screen.
What I want to achieve: After land in home screen, programmatically call api to refresh my balance. Route Observer able to detect navigation back to home screen with didPopNext() method. But this is called no matter it pop from which screen. Therefore the api will repeatedly called which is not ideal. How do I know it was pop from purchased successful screen instead of product listing screen?
Grateful on any helps and hints!
If you where using pop() then you have an option to return some data to previous route but since you are using popUntil() you won't be able to do this. So I think you should clear every route and push home route again from purchased successful page using either pushAndRemoveUntil() or pushNamedAndRemoveUntil(). That means you can now pass an argument to home screen by which you can decide whether to call the API or not.
Navigator.of(context).pushNamedAndRemoveUntil(
'/',
(Route<dynamic> route) => false,
arguments: []
);
Another option would be to figure out a way to return data using popUntil(), Something like this, maybe?
I want my application to a have Welcome screen, with registration and login option. When I tap on "registration" button to navigate to my Registration screen, i use:
Navigator.push
When I press the "back" button within the Registration screen, it must return to the Welcome page.
But after the user completes the registration I'm using the following code in the Homepage to navigate to my home.
Navigator.pushReplacement
The problem is, when I pressed the "back" button, my app is coming back to the Welcome screen, instead of staying within the Home.
Any idea what is going on?
You can use the following method to clear the navigation stack when you navigate:
Navigator.of(ctx).pushAndRemoveUntil(YourRoute, (Route<dynamic> route) => false);
The second parameter is a Predicate to decide weather the route will be deleted from the stack or not.
Using this method, you'll clear the whole stack before navigating to the route, that way, you won't be able to go back to your Welcome page.