我在屏幕A中使用GetX控制器并导航到屏幕B,这也是一个具有不同数据但相同控制器的相同屏幕,(这是在社交媒体应用程序中用户配置文件内部和内部导航的概念-参考:Instagram)
问题是当我从屏幕A导航到屏幕B时,屏幕B数据是从API中获取的,这是完全好的,然后当我们点击返回按钮时,它返回到屏幕A,但屏幕A数据被屏幕B数据取代
初始化控制器,如
final ProfileViewController userProfileController =
Get.put(ProfileViewController());
像
一样导航到屏幕Get.to(() => ScreenB());
期待最好的解决方案,提前感谢!
我这样做了,它工作得很好,希望它有帮助
在main.dart
Get.create(() => ProfileViewController());
then in view
ProfileViewController_con = Get.find();
Get.to(() => const ScreenB(), preventDuplicates: false);
尝试在GetX中找到并使用GetWidget
使用GetWidget,您将为每个小部件实例拥有一个单独的新控制器。
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(),
),
]
使用tag
与控制器
像
那样初始化控制器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')
通过这种方式,可以创建和访问多个配置文件控制器