I’m creating an app in flutter and i added login to it, but while logout the home bar stays there while also showing the new login page.
So below is the code for logout
class profile extends StatelessWidget {
profile({super.key});
final _myBox = Hive.openBox("mybox");
final loggedModel log = loggedModel();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
height:100,
width: 100,
child: ElevatedButton(
onPressed: ()=> {
log.out = true,
Navigator.pop(context),
Navigator.push(context, MaterialPageRoute<void>(
builder: (BuildContext context) => const Splash(),
),)
},
child: Text("LOGOUT")
),
),
),
);
}
}
so after login the it returns a widget of homescreen which has cupertinotabscaffold, and in that there’s one page of profile containing the logout button. so when i logout the logout page is loaded but the cupertino tab stays there as shown in pics below
and this is the profile with logout button
and here you can see that after logout the tab is still present and im not able to pop it out or remove it
i tried poping the homescreen or closing the app itself after logging out but they are not working
2
Answers
Push to new screen and remove all previous screen
Push to new route and remove all previous routes
pushAndRemoveUntil
will remove all existing routes and push to new page.It seems like you’re trying to log out from your application by popping the current route from the navigator stack and then pushing a new route. However, the problem you’re encountering is that the previous routes are still visible.
To resolve this issue, you should navigate to the login page and clear the entire navigation stack when the user logs out. Here’s how you can do
it:
In this modified version, I’ve replaced Navigator.pop(context) with Navigator.pushAndRemoveUntil() method. This method will push the new login page onto the navigation stack and then remove all routes from the stack except for the new login page, effectively clearing the navigation history.
This way, when the user logs out, they’ll be taken directly to the login page without any previous routes remaining visible.