我使用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);
}
只需使用Navigator
if(response.body == 'Login successful'){
isLogin = true;
Navigator.of(context).pushReplacementNamed('/home');
}else{
isLogin = false;
}
您可以在此处了解更多信息:https://flutter.dev/docs/cookbook/navigation/named-routes
您也可以从最近的回答中尝试我的ConditionalRouter
实现:在Flutter 中创建私有路由