I'm trying to implement a NavigatorObserver in order to execute a method each time a new entry is pushed/popped on the Navigation stack.
First of all, here is my Observer:
class NotificationNavigatorObserver extends NavigatorObserver {
NotificationNavigatorObserver(this._notifProvider);
final NotificationProvider _notifProvider;
@override
void didPush(Route route, Route? previousRoute) {
super.didPush(route, previousRoute);
print("didPush()");
_notifProvider.refreshNotifications().then((value) => null);
}
@override
void didPop(Route route, Route? previousRoute) {
super.didPop(route, previousRoute);
print("didPop()");
_notifProvider.refreshNotifications().then((value) => null);
}
}
And here is how I implemented it in my main App:
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => NotificationProvider()),
ChangeNotifierProvider(create: (_) => ModalProvider()),
ChangeNotifierProvider(create: (_) => FullScreen()),
],
builder: (context, child) => Builder(
builder: (context) {
final notificationProvider = Provider.of<NotificationProvider>(context, listen: false);
return Consumer<FullScreen>(
builder: (context, fullScreen, child) => MaterialApp(
navigatorObservers: [
NotificationNavigatorObserver(notificationProvider)
],
...
The issue is that none of the Observer's methods are called when they should be. The only related event that happens is a "didPush()" printing in the console on app launch. Nothing else.
For more context, this is how the user navigate in this app:
Navigator.push(context, MaterialPageRoute(builder: (context) => SomePage()));
I don't understand what I'm doing wrong. Where should I investigate?