0

In the MaterialApp constructor, I am injecting a widget in the builder method to handle push notifications. To process foreground notification, I would like to use this widget to show a notification bar (using another_flushbar) but I need a Navigator in the context for that, and MaterialApp's builder injects above the Navigator, so when I try to using it to push my notification bar, there is no Navigator accessible via the context.

Of course, I could wrap every screen of my app with my notification-handling widget, but it defeats the purpose. I want a widget that is always on screen when the app is running, and that can show my notification bar, wherever I am in my app.

Any idea how I can do that?

Sebastien
  • 3,583
  • 4
  • 43
  • 82

1 Answers1

0

Here is how I solved it, while still using MaterialApp's builder to inject my NotificationHandler widget ABOVE the Navigator.

  1. My NotificationHandler widget accepts a GlobalKey<NavigatorState> navigatorKey property
  2. When I initialize my NotificationHandler widget inside of MaterialApp's builder, I pass it GoRouter.routerDelegate.navigatorKey
  3. Inside of NotificationHandler, when I need a context with a Navigator above it, I use navigatorKey.currentContext insted of my local widget context.

This solution was inspired by this issue.

Sebastien
  • 3,583
  • 4
  • 43
  • 82