1、在build中使用Get.put(),然后在页面controller的onReady中进行页面跳转,某些时候页面会执行多次
[GETX] Instance "GetMaterialController" has been initialized
[GETX] GOING TO ROUTE /splash
[GETX] Instance "SplashController" has been created
[GETX] Instance "SplashController" has been initialized
[GETX] GOING TO ROUTE /home
[GETX] REMOVING ROUTE /splash
[GETX] Instance "BottomNavigationController" has been created
[GETX] Instance "BottomNavigationController" has been initialized
[GETX] Instance "DashboardController" has been created
[GETX] Instance "DashboardController" has been initialized
[GETX] Instance "SplashController" has been created
[GETX] Instance "SplashController" has been initialized
解决:
通常情况下,使用Get.put来注入控制器是在build方法中完成的,因为Get.put通常用于在控件树中创建并提供控制器的实例。然而,在某些情况下,如果不小心将Get.put放在了build方法中,并且在控制器的onReady方法中进行了页面跳转,可能会导致问题,因为页面跳转通常会导致build方法重新调用,从而再次触发控制器的创建和onReady方法的调用,这可能会导致多次跳转的情况。
为了解决这个问题,您可以在以下两种方式中选择一种:
1、将Get.put的调用移动到initState中:将Get.put(SplashController())移动到SplashPage的initState方法中。这样,控制器将在页面第一次创建时进行初始化,并且不会在页面重建时再次创建。
2、使用init参数:在GetBuilder或Get.create中使用init参数初始化控制器,以确保只在首次创建控件时创建控制器。例如:GetBuilder<SplashController>(init: SplashController())。