I have this problem when I have Nested Navigators. So something like,
class App extends StatelessWidget {
@override
Widget build(BuildContex
Most of the time, you'll have only 2 Navigator.
Which means to obtain the nested one, do:
Navigator.of(context)
And to obtain the root one do:
Navigator.of(context, rootNavigator: true)
For more complex architecture, the easiest by far is to use GlobalKey (since you'll never read Navigators during build)
final GlobalKey key =GlobalKey();
final GlobalKey key2 =GlobalKey();
class Foo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: key,
home: Navigator(
key: key2,
),
);
}
}
Which you can then use this way:
key.currentState.pushNamed('foo')