Clarify my doubt
Clarify my doubt

Reputation: 59

By Clicking back button screen is navigating to home screen after logging out

After clicking the logout button from home I am using the below line to navigate to the login screen. But after navigating to the login screen, if I click the back button it is navigating to the home screen. I want all screens to pop and the app should close if I click the back button from the login screen.

Both the lines didn't work for me. Help if someone knows this... Navigator.of(context).pushReplacementNamed(LOGIN); Navigator.of(context).pushNamedAndRemoveUntil(LOGIN, (route) => false);

Upvotes: 2

Views: 3223

Answers (1)

dzenan9999
dzenan9999

Reputation: 43

First of all, you are obviously not checking if the user is logged in because you should not be able to access the HOME page after you log out. You can check this either in your api calls or, for example, in initState() method of HOME page to automatically reroute user to LOGIN page.

Back button is redirecting you to HOME page because you need to first clear all paths before going to LOGIN page. Try this:

onPressed:(){
    clearSession();
      Navigator.pop(context,true);
      Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => LoginScreen()),
);

},

If there is no previous routes, the app will get closed when the back button is pressed. However, if you want to explicitly tell Flutter to close the app on back button you can do something like this https://www.codegrepper.com/code-examples/dart/flutter+close+app+on+back+button

Upvotes: 2

Related Questions