0

I am trying global context. But showed error. How to fix?

My code.

final _appRouter = AppRouter(); ///Used auto_route

MaterialApp.router(
routerConfig: _appRouter.config(navigatorObservers: () => [HeroController()]),
scaffoldMessengerKey: scaffoldMessengerKey,
builder: (context, child) {
    globalContext = context;

return builder == null`enter code here`
                      ? (child ?? const Material())
                      : builder!(context, child ?? const Material());
                });

///--Used globalContext 
showDialog(
context: globalContext!,
builder: (BuildContext context) {
return DialogDesignSimple(
child: child, title: title, maxWidth: maxWidth);
});


///---Used scaffoldMessengerKey
showDialog(
context:  scaffoldMessengerKey!.currentContext,
builder: (BuildContext context) {
return DialogDesignSimple(
child: child, title: title, maxWidth: maxWidth);
});

Error output

Another exception was thrown: Navigator operation requested with a context that does not include a Navigator.

DG Hub
  • 9
  • 2

1 Answers1

0

Why not just use context: context instead? you can pass that from any builder.

The error is saying something is wrong with your context which is in this case, globalContext. I wouldn't recommend using a global context but if you really insist you can check out this post to create a navigation key:

Get the Global Context in Flutter

Texv
  • 1,225
  • 10
  • 14