The question may be duplicate but I'm unable to found the solution. My app scenario is the same as the almost app. My first screen is Splash screen and holds for two seconds and here login session is checked and upon this condition screen changes like this and below code is run in initState()
_checkPreference() async {
PreferencesConnector myprefs= PreferencesConnector();
id=await myprefs.readString('merchantid');
if(id==null||id==''){
Future.delayed(
const Duration(seconds: 2),
() => Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => Login(),settings: RouteSettings(name: '/login')),
));
}else{
Future.delayed(
const Duration(seconds: 2),
() => Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => DashBoard()),
));
}
}
If the session returns false then it goes to login
screen, In Login screen my scaffold
inside in WillPopScope
widget and that class is stateful
class
return WillPopScope(
onWillPop: () {
if (Navigator.canPop(context)) {
//Navigator.pop(context);
Navigator.of(context).pop();
} else {
SystemNavigator.pop();
}
},
child:Scaffold(
body: Stack(
children: <Widget>[
and if LoginApi returns true then it move to dashboard
like this
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => DashBoard(),
settings: RouteSettings(name: '/dashboard')),
);
here everything is working fine when the user is already logged-in and we reach to dashboard after the splash and but there is a logOut
button on my dashboard when user press logout
then there is a dialog appear which asks for logout- if i press yes from dialog button works like this
onPressed:(){
clearSession();
// Navigator.of(context).popUntil(ModalRoute.());
// Navigator.of(context).popUntil('/login', (Route<dynamic> route) => false);
// Navigator.popUntil(context, ModalRoute.withName("/login"));
// Navigator.of(context).pushNamedAndRemoveUntil('/login', (Route<dynamic> route) => false);
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Login()),);
},
after pressing logout it reaches to login screen where I'm unable to close the app when user press back from the device back in login screen but it redirects to the dashboard and then I pressed back then app closed.
What you need to do is first clear all path before going to login()
screen.
try this:
onPressed:(){
clearSession();
//Navigator.popUntil(context, ModalRoute.withName('/'));
Navigator.pop(context,true);// It worked for me instead of above line
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Login()),);
},
call this on your logout button
void _logout() {
Navigator.popUntil(context, ModalRoute.withName('/login'));
}
here is the link of official docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With