I have a Maintenance controller, that initializez an observable variable, and whenever that variable is true
, it navigates to a maintenance screen, and when the variable gets set to false it navigates back as follows:
class MaintenanceController extends GetxController {
RxBool isMaintenance = false.obs;
@override
onInit() {
super.onInit();
ever(
isMaintenance,
(_) => {
if (isMaintenance.value) {Get.offNamed(Routes.maintenance)} else {Get.offNamed(Routes.splash)}
});
}
}
Everything works fine, but I get the following exception in the logs:
flutter: You are trying to use contextless navigation without
a GetMaterialApp or Get.key.
If you are testing your app, you can use:
[Get.testMode = true], or if you are running your app on
a physical device or emulator, you must exchange your [MaterialApp]
for a [GetMaterialApp].
flutter:
#0 GetNavigation.global (package:get/get_navigation/src/extension_navigation.dart:1094:7)
#1 GetNavigation.offNamed (package:get/get_navigation/src/extension_navigation.dart:629:12)
#2 MaintenanceController.onInit.<anonymous closure> (package:n_app/src/screens/maintenance/maintenance_controller.dart:17:85)
#3 ever.<anonymous closure> (package:get/get_rx/src/rx_workers/rx_workers.dart:69:44)
#4 GetStream._notifyData (package:get/get_rx/src/rx_stream/get_stream.dart:47:21)
#5 GetStream.add (package:get/get_rx/src/rx_stream/get_stream.dart:97:5)
#6 RxObjectMixin.value= (package:get/get_rx/src/rx_types/rx_core/rx_impl.dart:105:13)
#7 MaintenanceController.subscribe.<anonymous closure> (package:n_app/src/screens/maintenance/maintenance_controller.dart:29:21)
#8 _rootRunUnary (dart:async/zone.dart:1434:47)
#9 _CustomZone.runUnary (dart:async/zone.dart:1335:19)
#10 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
#11 _Bufferi<…>
flutter: ----------------------------------------------------
app.dart:
GetMaterialApp(
debugShowCheckedModeBanner: false,
locale: Get.find<LocalizationController>().deviceLocale,
getPages: Routes.routes,
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
unknownRoute: Routes.getNotFound,
initialRoute: GetPlatform.isWeb ? Routes.initial : Routes.splash,
theme: Get.find<ThemeController>().themeData,
navigatorKey: NavigationService.navigatorKey,
);
I tried to add the navigatorKey: Get.key
and use navigatorKey.currentState!.pushNamed(routeName);
to navigate, but the currentState is always null
. Any advice?
Adding navigatorKey: Get.key
to the GetMaterialApp and continuing to use Get.offNamed(...)
instead of navigatorKey.currentState!.pushNamed(routeName);
worked.