I’m building a flutter app and I have a main page, but it also has a login and logout page.
So, if you’re not logged in, the main page is replaced with login. Should I use pushAndRemoveUntil, so logged in users can’t press back into the login page? And vice versa, if they’re logged out, should I use pushAndRemoveUntil so you can’t back into the login page? I check for session to make sure user logged in, so the main page won’t work, but curious what’s the best approach. Curious if this is a hammer and scalpel problem, but I’m not sure which is which.
2
Answers
pushAndRemoveUntil
is used to remove routes until certain condition is true, usually, if the removing procedure reaches the route specified in the logic:if the condition just retunrs
true
all the time, it will remove all theroutes
from thestack
, in other words all theroutes
that are behind thepushed route
.PopScope
is just to create a some intentional behaviour when user tries to go back/pop the route
from thenavigation stack
.PopScope
can be used to restrict popping the route at all.So there are certain scenarious that you have:
push and remove everything
behind, including login page.PopScope
asredundant restriction
for the user to not go back, which might happen if you don’t remove all routes when user logs out.PopScope's
condition. But the best would be to use both, so, when user logs out remove all the routes withpushAndRmoveUntil
. and just usePopScope
on log in page for the redundant restriction, just to be safe.To sum up: you need to utilize both.
when login:
pushAndRemoveUntil
When log out:
pushAndRemoveUntil
When on login page:
PopScope
(restricting the popping)pushAndRemoveUntil
is about pushing a new route and clearing other routes from the navigation stackpopScope
the back button press (either from the Android hardware button or the back button in the app bar) to ask forconfirmation
orprevent
the app from popping the current route from the navigation stack.You can use
pushAndRemoveUntil
together withpopScope
to prevent the user from popping the route, because attempting to pop after usingpushAndRemoveUntil
can result in a black screen.learn more about pushAndRemoveUntil
learn more about Pop scope