我使用MongoDB在Flutter中创建了一个登录页面,当用户输入有效的凭据时,必须将其导航到首页,否则它们应该留在同一页面中。我该如何实现这种逻辑?
当他们输入有效的凭据时,我可以在控制台中成功打印用户,但是如果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);
}
答案 0 :(得分:4)
只需使用Navigator
if(response.body == 'Login successful'){
isLogin = true;
Navigator.of(context).pushReplacementNamed('/home');
}else{
isLogin = false;
}
您可以在此处了解更多信息:https://flutter.dev/docs/cookbook/navigation/named-routes
您也可以从最近的答案Making Private Route in Flutter
中尝试我的ConditionalRouter
实现