dart Flutter GetX绑定?

cqoc49vn  于 5个月前  发布在  Flutter
关注(0)|答案(3)|浏览(69)

我是新来的这个架构,我有一个关于getX绑定的疑问
我有两个控制器cartController和splashController我需要这个cartController保持其状态,因为应用程序的整个生命周期splashController只为splashScreen
但是splash控制器只有在GetMaterialApp的初始绑定中与其他控制器绑定时才有效

GetMaterialApp(
      home: const SplashScreen(),
      initialBinding: RootBinding(),
      getPages: [
        GetPage(
          name: SplashScreen.routeName,
          page: () => const SplashScreen(),
        ),
        GetPage(
            name: HomeScreen.routeName,
            page: () => const HomeScreen(),
            children: [
              GetPage(
                name: CategoryScreen.routeName,
                page: () => const CategoryScreen(),
              ),
              GetPage(
                name: AboutScreen.routeName,
                page: () => const AboutScreen(),
              ),
              GetPage(
                name: CartScreen.routeName,
                page: () => const CartScreen(),
              ),
            ]),
      ],
    );

字符串
根绑定是

class RootBinding implements Bindings {
  @override
  void dependencies() {
    Get.put<SplashController>(SplashController());
    Get.put<HomeController>(HomeController());
    Get.put<CategoryController>(CategoryController());
    Get.put<AboutController>(AboutController());
  }
}


当我将它改为Get.lazyPut()时,它也不起作用。
我不知道这是否是最好的做法,上面的代码工作,但当我删除一个控制器从初始绑定到一个页面,它不像下面这样工作

GetPage(
   name: SplashScreen.routeName,
   page: () => const SplashScreen(),
   binding: SplashBinding()
   ),


源代码https://github.com/shabhi1997/GetXBaseProject/tree/master

uemypmqf

uemypmqf1#

您可以使用BindingsBuilder回调。每当您访问闪屏时,Getx都会创建一个闪屏控制器示例,一旦页面将从stack中删除,GetX就会自动删除闪屏控制器示例。

GetPage(
   name: SplashScreen.routeName,
   page: () => const SplashScreen(),
   binding: BindingsBuilder(() {
          Get.lazyPut<SplashController>(
            () => SplashController(),
          );
        }))

字符串

2nc8po8w

2nc8po8w2#

我也做过类似的事情,希望这对你有帮助。
当您在启动屏幕后将用户导航到主屏幕时,您可以使用Get.offAll()进行导航,其中绑定已初始化所有控制器
导航到主屏幕

Get.offAll(
  () => const InitialScreen(screenTag: appControllersTag),
  binding: InitialScreenBindings(tag: appControllersTag),
)

字符串
初始化所有控制器

class InitialScreenBindings implements Bindings {
  String tag;

  InitialScreenBindings({required this.tag});

  @override
  void dependencies() {
    Get.lazyPut(() => BottomNavigationController(), tag: tag);
    Get.lazyPut(() => HomeController(), tag: tag);
    Get.lazyPut(() => OrderController(), tag: tag);
    Get.lazyPut(() => CartController(), tag: tag);
    Get.lazyPut(() => ProfileController(), tag: tag);
  }
}


有了这个,我的BottomNavigationController、HomeController、OrderController、CartController和ProfileController在应用程序的整个生命周期中都处于活动状态
快速建议:你可以注入带有标签的控制器,相信我,这会让你的生活变得轻松,因为它有助于调试和修复问题。

v6ylcynt

v6ylcynt3#

在cartController的情况下,只需在Get.put()方法中设置永久标志:true,以使其在整个应用程序循环中保持活动状态。
同时删除home:const SplashScreen()。这是导致主要问题的原因。创建splashScreen时没有绑定(控制器)。
相反,在声明应用程序的路由时,给予SplashScreen路由名称“/”,这意味着初始路由。它将在开始时自动初始化。您不需要在初始绑定中提供它。

相关问题