[英]GetX Controller in Stack (Flutter)
我在屏幕 A 中使用 GetX Controller 并导航到屏幕 B,这也是具有不同数据但相同的屏幕 B,但相同的 Controller,(这是在社交媒体应用程序的用户个人资料内部和内部导航的概念 - 参考:Instagram)
问题是当我从屏幕 A 导航到屏幕 B 时,屏幕 B 数据是从 API 获取的,这完全没问题,然后当我们单击后退按钮时它返回到屏幕 A,但屏幕 A 数据被屏幕 B 数据替换
初始化controller like
final ProfileViewController userProfileController =
Get.put(ProfileViewController());
导航到屏幕像
Get.to(() => ScreenB());
期待最好的解决方案,在此先感谢!
我这样做了,效果很好,希望对您有所帮助
在 main.dart
Get.create(() => ProfileViewController());
然后在视图中
ProfileViewController_con = Get.find();
Get.to(() => const ScreenB(), preventDuplicates: false);
尝试在 GetX 中查找并使用 GetWidget
使用 GetWidget,每个小部件实例都会有一个单独的新 controller。
class ProfileViewWidget extends GetWidget< ProfileViewController > {...}
...
class YourAnyScreenBindings implements Bindings {
@override
void dependencies() {
Get.put(YourAnyScreenCtrl());
Get.create(() => ProfileViewController());
}
}
...
List<GetPage<dynamic>> getPages = [
GetPage(
name: '/your_any_screen',
page: () => YourAnyScreen(),
binding: YourAnyScreenBindings(),
),
]
使用带有 controller 的tag
像初始化controller
final ProfileViewController userProfileController =
Get.put(ProfileViewController(),tag:'a_string_may_be_the_profile_id');
用法:
var controller = Get.find<ProfileViewController>(tag:'a_string_may_be_the_profile_id')
通过这种方式,可以创建和访问多个配置文件 controller
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.