[英]Navigate to next page based on a condition in flutter
I created a signIn page in flutter using MongoDB, when user enters valid credentials they have to be navigated to HomePage, else they should stay in the same page.我使用 MongoDB 在 flutter 中创建了一个登录页面,当用户输入有效凭据时,他们必须导航到主页,否则他们应该留在同一页面。 How can I achieve this logic?我怎样才能实现这个逻辑?
when they enter valid credentials I am able to print user successful in the console, but how can I navigate to next page if it is isLogin = true?当他们输入有效凭据时,我可以在控制台中成功打印用户,但是如果 isLogin = true,我如何导航到下一页?
Future<String> signIn() async {
final response = await http.post(
serverReceiverPath,
headers: {'Content-Type': 'application/json'},
);
if(response.body == 'Login successful'){
isLogin = true;
}else{
isLogin = false;
}
print(response.body);
print(isLogin);
}
Just use Navigator
只需使用Navigator
if(response.body == 'Login successful'){
isLogin = true;
Navigator.of(context).pushReplacementNamed('/home');
}else{
isLogin = false;
}
You can learn more about it here: https://flutter.dev/docs/cookbook/navigation/named-routes你可以在这里了解更多信息: https : //flutter.dev/docs/cookbook/navigation/named-routes
You can also try my ConditionalRouter
implementation from this recent answer: Making Private Route in Flutter您还可以从最近的答案中尝试我的ConditionalRouter
实现: Making Private Route in Flutter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.