[英]Flutter GETX: How to remove Initialized Controller every time we navigate to other page/routes
[英]Flutter: Can we navigate to other screen inside controller using GetX?
我可以像下面的代碼一樣在 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?
});
}
}
答案是可以的。 只需在您的方法中傳遞構建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?
});
}
是的,您可以在 controller 文件中導航,只需替換
Get.to(OtherScreen());
至
Get.to(()=> OtherScreen());
兩者都工作,但第二個是使用 Getx 導航的正確語法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.