Flutter - Navigator.push() giving error "Context does not include a Navigator" on extends State<Main>
- Create a global key for the navigator so you can access it from anywhere in the application:
final GlobalKey<NavigatorState> myNavigatorKey = GlobalKey<NavigatorState>();
- Assign it to your app when creating
MaterialApp
:
return MaterialApp(navigatorKey: myNavigatorKey,...);
- Use it when pushing routes:
myNavigatorKey.currentState?.pushNamed(...);