Context Navigator Not Working in main.dart (CNNWM)



AVAILABLE:    5

I encountered this unique case when trying to navigate a push notification from the main page to a related page. However, it seemed that the context in main.dart was not compatible, as if the navigator was never executed.

Solution

I eventually used the didChangeAppLifecycleState method, which does not provide any context, unlike the build method. You need to navigate without using context by setting a global key for your navigation:

final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

Setting the navigatorKey in MaterialApp

MaterialApp(
  title: 'MyApp',
  onGenerateRoute: generateRoute,
  navigatorKey: navigatorKey,
);

Execute navigation like this:

navigatorKey.currentState.pushNamed('/someRoute');

Done!


Post a Comment

Previous Next

نموذج الاتصال