简体   繁体   English

堆栈中的 GetX Controller(Flutter)

[英]GetX Controller in Stack (Flutter)

Im using GetX Controller in Screen A and navigating to the Screen B which is also a same screen with different data's but same Controller, (its a concept of navigating inside and inside the users profile in social media app - reference: Instagram)我在屏幕 A 中使用 GetX Controller 并导航到屏幕 B,这也是具有不同数据但相同的屏幕 B,但相同的 Controller,(这是在社交媒体应用程序的用户个人资料内部和内部导航的概念 - 参考:Instagram)

The issue is when i navigates from Screen A to Screen B, Screen B data is fetched from the API which is totally fine, then when we clicks back button it return back to Screen A, but the Screen A data is replaced with Screen B data问题是当我从屏幕 A 导航到屏幕 B 时,屏幕 B 数据是从 API 获取的,这完全没问题,然后当我们单击后退按钮时它返回到屏幕 A,但屏幕 A 数据被屏幕 B 数据替换

Initialized controller like初始化controller like

 final ProfileViewController userProfileController =
      Get.put(ProfileViewController());

Navigating to screen like导航到屏幕像

Get.to(() => ScreenB());

Expecting the best solution, Thanks in Advance!期待最好的解决方案,在此先感谢!

I did this and it worked fine, hope it helps我这样做了,效果很好,希望对您有所帮助

in main.dart在 main.dart

Get.create(() => ProfileViewController());

then in the view然后在视图中

ProfileViewController_con = Get.find();

Get.to(() => const ScreenB(), preventDuplicates: false);

Try to find and use GetWidget in GetX尝试在 GetX 中查找并使用 GetWidget

With GetWidget, you will have a separate new controller for each widget instance.使用 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(),
  ),
]







Use tag with the controller使用带有 controller 的tag

Initialize controller like像初始化controller

final ProfileViewController userProfileController =
  Get.put(ProfileViewController(),tag:'a_string_may_be_the_profile_id');

usage:用法:

var controller = Get.find<ProfileViewController>(tag:'a_string_may_be_the_profile_id')

By this way, more than one profile controller can be created and accessed通过这种方式,可以创建和访问多个配置文件 controller

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM