0

I found the below error's explanation in this link: Navigator operation requested with a context that does not include a Navigator I tried to apply to my problem but I couldn't solve the problem.

When I click the button in the NavigationBar class, I got this error:

The following assertion was thrown while handling a gesture: Navigator operation requested with a context that does not include a Navigator. The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.

void main() async {
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();

    AppRouter appRouter = AppRouter(
      routes: AppRoutes.routes,
      notFoundHandler: AppRoutes.routeNotFoundHandler,
    );

    appRouter.setupRoutes();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'abc',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      builder: (_, child) => AppView(
        child: child,
      ),
      onGenerateRoute: AppRouter.router.generator,
      );
  }
}

class AppView extends StatelessWidget {
  
final Widget child;
  
const AppView({@required this.child});
  
@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [NavigationBar(), Expanded(child: child)],
      ),
    );
  }
}

class NavigationBar extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Container(
            child: MaterialButton(
              child: Text(
                "Home",
              ),
              onPressed: () {
                AppRouter.router
                    .navigateTo(context, AppRoutes.saticilarRoute.route);
              },
            ),
          ),
           //TODO
        ],
      ),
    );
  }
}
John Montana
  • 175
  • 10

1 Answers1

0

The reason why this problem occurred:

Any widget returned by the builder will be the parent of the navigator.

enter image description here

If you want the persistent navigation bar across all pages then you can use builder.

// Need to pass the child to navigation bar which is not a good solution
class NavigationBar extends StatelessWidget {
const NavigationBar({Key key, this.child}: super(key:key))
final Widget child;

// Replace this
AppRouter.router.navigateTo(context, AppRoutes.saticilarRoute.route);

// with
(child.key as GlobalKey<NavigatorState>).currentState.pushNamed(AppRoutes.saticilarRoute.route)

Persisting AppBar Drawer across all Pages Flutter

or you can use a navigation key, check out this. How to navigate without context in flutter app?

Even better is to put navigation bar in a separate file and reuse that if needed. Remove builder and use home/initialRoute.

Ninja
  • 366
  • 2
  • 7