Global context in Flutter while using MaterialApp.router()

Viewed 21

I want to know if there is a way to use a Global Context in a Flutter App while using auto route as MaterialApp.router() don't have a navigatorKey.

1 Answers

Specify scaffoldMessengerKey to MaterialApp:

final snackbarKey = GlobalKey<ScaffoldMessengerState>();

...

MaterialApp(
  scaffoldMessengerKey: snackbarKey,

You can get the context from GlobalKey:

snackbarKey.currentState?.context
Related