I am following raywenderlich's flutter apprentice tutorial (Chapter 7 https://www.raywenderlich.com/books/flutter-apprentice/v1.0/chapters/7-routes-navigation) and I am not able to understand what the call to addListener(notifyListener) means in the following code. Specifically, I am thinking that addListener should be taking in a Listener object but here, it is taking a function which is defined in the super class!
class AppRouter extends RouterDelegate
with ChangeNotifier, PopNavigatorRouterDelegateMixin {
// 3
final AppStateManager appStateManager;
// 4
final GroceryManager groceryManager;
final ProfileManager profileManager;
AppRouter({
required this.appStateManager,
required this.groceryManager,
required this.profileManager,
}) : navigatorKey = GlobalKey<NavigatorState>() {
appStateManager.addListener(notifyListeners);
groceryManager.addListener(notifyListeners);
profileManager.addListener(notifyListeners);
}
...
}
Any idea what is going on and how it is working?
CodePudding user response:
Essentially, the addListener
functions adds a listener, that triggers the functions that's provided. So essentially, every time the XXXStateManager
state changes, it will trigger the notifyListeners
method from the ChangeNotifier
class