Flutter - I get a GlobalKey error when using a NavigatorObserver

36 Views Asked by At


I have a method I need to call everytime a new route is pushed/popped in the Navigator stack. Basically, everytime the user goes from a screen to another.

I've read that the best way to do this was by using the NavigatorObserver. I tried to implement it that way :

class NavigationObserver extends NavigatorObserver {
  @override
  void didPush(Route route, Route? previousRoute) {
    super.didPush(route, previousRoute);

    NotificationsViewState().updateNotifs(); // "Notifications" page
    NotifScrollContentState().updateNotifs(); // content of the notifications modal
  }
}

Using this class, I implemented it in my main MaterialApp :

MaterialApp(
    navigatorObservers: [NavigationObserver()],
    // etc...

This resulted in an error implying that the GlobalKeys might be at cause there :

A GlobalKey was used multiple times inside one widget's child list.
The offending GlobalKey was: [GlobalObjectKey_WidgetsAppState#263c0]
The parent of the widgets with that key was: _FocusInheritedScop
The first child to get instantiated with that key became: Navigator-[GlobalObjectKey_WidgetsAppState#263c0]
The second child that was to be instantiated with that key was: _FocusInheritedScope
A GlobalKey can only be specified on one widget at a time in the widget tree.

I am no Dart expert, so I do struggle to understand how could I fix this issue. FYI, the only GlobalKeys I'm using in my Flutter app are only to implement AnimatedLists, so I don't think that's related.

0

There are 0 best solutions below