skip to Main Content

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


  1. pushAndRemoveUntil is used to remove routes until certain condition is true, usually, if the removing procedure reaches the route specified in the logic:

    remove unti: route.name = "/home"
    

    if the condition just retunrs true all the time, it will remove all the routes from the stack, in other words all the routes that are behind the pushed route.

    PopScope is just to create a some intentional behaviour when user tries to go back/pop the route from the navigation stack. PopScope can be used to restrict popping the route at all.

    So there are certain scenarious that you have:

    1. When user logs in you need to push and remove everything behind, including login page.
    2. when user is on the login page, you might have PopScope as redundant restriction for the user to not go back, which might happen if you don’t remove all routes when user logs out.
    3. When user logs out, better practice will be to remove all the routes from the stack and push user to the log in page. you can also, just push user to login page, leave all other routes and restrict the navigation back with PopScope's condition. But the best would be to use both, so, when user logs out remove all the routes with pushAndRmoveUntil. and just use PopScope 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)

    Login or Signup to reply.
  2. pushAndRemoveUntil is about pushing a new route and clearing other routes from the navigation stack

    popScope the back button press (either from the Android hardware button or the back button in the app bar) to ask for confirmation or prevent the app from popping the current route from the navigation stack.

    You can use pushAndRemoveUntil together with popScope to prevent the user from popping the route, because attempting to pop after using pushAndRemoveUntil can result in a black screen.

    learn more about pushAndRemoveUntil

    learn more about Pop scope

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search