[英]Flutter: Can we navigate to other screen inside controller using GetX?
Can I use Get.to(OtherScreen()) inside Getx controller like the code below?我可以像下面的代码一样在 Getx controller 中使用Get.to(OtherScreen())吗?
class UserController extends GetxController{
Future<void> loginWithEmailAndPassword(
{@required String email, @required String password}) async {
await _firebaseAuth.signInWithEmailAndPassword(
email: email.toLowerCase(),
password: password,
).then((credential){
Get.to(OtherScreen()); // Can I use this inside controller?
});
}
}
Answer is yes you can.答案是可以的。 Just pass the build context
in your method and then use the navigation route只需在您的方法中传递构建context
,然后使用导航路线
Future<void> loginWithEmailAndPassword(
{@required String email, @required String password,@required BuildContext context}) async {
await _firebaseAuth.signInWithEmailAndPassword(
email: email.toLowerCase(),
password: password,
).then((credential){
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => OtherScreen(),
),; // Can I use this inside controller?
});
}
Yes you can navigate in controller file just replace是的,您可以在 controller 文件中导航,只需替换
Get.to(OtherScreen());
to至
Get.to(()=> OtherScreen());
Both Working but second is right syntax to navigate using Getx两者都工作,但第二个是使用 Getx 导航的正确语法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.