2
I have the following login widget that after the request is successfully made, it adds on the navigation stack to the home screen and removes the login screen.
If I try to get back from home screen to login the app is minimized. However, when I open again the app opens the login screen.
In my main.dart
is checked if it has a token. If it does, it is directed to the home screen. If not, to the login screen.
checkHome() async {
// Read value
String token = await storage.read(key: 'token');
return token;
}
@override
void initState(){
super.initState();
checkHome();
}
@override
Widget build(BuildContext context) {
/// Chamada para solucionar o método.
/// Nele também é acionado o `then()` que é
/// que é onde pode ser capturado o retorno do futuro.
checkHome().then((token) {
temToken = token != null;
});
return MaterialApp(
title: 'Cadê meu pet?',
theme: ThemeData(primarySwatch: Colors.yellow),
/// Declaração do ternário para escolher qual page exibir
home: temToken ? HomePage() : LoginPage(),
routes: routes
);
}
}
This is the way I handle the navigation stack on login:
if (response.statusCode == 202) {
Map<String, dynamic> map = json.decode(response.body);
// Write values
await storage.write(key: 'token', value: map['token']);
await storage.write(key: 'foto', value: map['foto']);
await storage.write(key: 'nomeUsuario', value: map['nomeUsuario']);
Navigator.pushNamed(context, '/home');
Navigator.popUntil(context, ModalRoute.withName('/login'));
}
My setting:
I have a token, so I’m on the home screen; I click on the button "back" android; The app is minimized; I open the app again; The login screen is displayed.
By possessing token, the expected is that when the app is minimized, when returning open the home screen.
I recommend that you post a complete code so that it can be reproduced so that you can fully understand the context of your problem and point out where it can be optimized. In the current way that posted it is difficult to understand what was actually encoded in the methods and how you organized everything. Another detail is also posting what you have already tried to understand the problem, such as what you noticed when debugging your app?
– Leonardo Paim
@Leonardo thanks for the feedback, I’ll try to organize better
– veroneseComS