Flutter GetX重复打开页面时控制器onInit方法未触发求助
问题原因
设置preventDuplicates: true后,GetX会检查路由栈中是否已存在SearchProductPage的实例。如果存在,不会创建新页面实例,而是直接复用已有的页面和控制器。而控制器的onInit方法仅在实例首次创建时执行,所以重复跳转时不会触发。
解决方法
根据你的业务需求,可选择以下方案:
方案1:移除preventDuplicates: true
如果允许路由栈中存在多个SearchProductPage实例,直接删除该参数。每次跳转都会创建新页面和控制器实例,onInit会正常触发:
Get.to(() => const SearchProductPage(), arguments: [ { "newModel": 1, "brandType": 1, "xyz": 0, "pqr": 0, "label": "Product xyz" } ]);
注意:这种方式会导致路由栈不断增加,返回时需要多次点击返回键回到上一页,可根据业务场景决定是否使用。
方案2:使用Get.off/Get.offToNamed替换Get.to
如果不需要保留之前的SearchProductPage实例,用off系列方法替换to,它会替换当前路由栈顶的页面,每次都会创建新实例,同时避免栈中堆积同页面:
// 用Get.off替换Get.to Get.off(() => const SearchProductPage(), arguments: [ { "newModel": 1, "brandType": 1, "xyz": 0, "pqr": 0, "label": "Product xyz" } ]);
方案3:复用页面实例,手动处理参数更新
如果必须保留preventDuplicates: true(避免栈中多个同页面),可以不再依赖onInit,而是在控制器中添加参数更新方法,跳转时判断页面是否存在,直接调用该方法:
步骤1:在控制器中添加参数处理方法
class SearchProductController extends GetxController { // ... 现有代码 void handleNewArguments(List<Map<String, dynamic>> arguments) { if (arguments != null) { fromCategoryBlock.value = true; loadingFirstTime.value = true; searchResultLabel.value = arguments[0]["label"]; fetchSearchProducts(true).then((value) { loadingFirstTime.value = false; }); } } @override void onInit() async { super.onInit(); // 处理首次加载的参数 handleNewArguments(Get.arguments); } }
步骤2:修改跳转逻辑
// 检查当前页面是否是SearchProductPage if (Get.currentRoute == "/searchProduct") { // 获取已存在的控制器实例,调用更新方法 final controller = Get.find<SearchProductController>(); controller.handleNewArguments([ { "newModel": 1, "brandType": 1, "xyz": 0, "pqr": 0, "label": "Product xyz" } ]); } else { // 页面不存在时,正常跳转 Get.to(() => const SearchProductPage(), arguments: [ { "newModel": 1, "brandType": 1, "xyz": 0, "pqr": 0, "label": "Product xyz" } ], preventDuplicates: true); }
备选:监听路由参数变化
也可以在控制器中监听路由变化,自动处理新参数:
@override void onInit() async { super.onInit(); handleNewArguments(Get.arguments); // 监听路由变化,当当前页面是目标页面时处理新参数 ever(Get.routing, (route) { if (route.current == "/searchProduct") { handleNewArguments(Get.arguments); } }); }
内容的提问来源于stack exchange,提问作者WatsMyName
相关产品推荐
相关产品推荐

