Chapter 7 ChangeNotifier

Reading through chapter 7, and it is confusing.

Why are we adding the notifyListeners on page 291 whereas notifyListeners() is already being called after each change within the respective state managers???
For example, the logout() method in app_state_manager.dart calls the notifyListeners() method immediately after it has effected a state change; so, why do we need to add the listeners as we do on page 291?

Also, it is not clear to me how AppRouter is listenning to the changes since we haven’t added a consumer that wraps AppRouter.

thanks

I believe it’s “listening” via the refreshListenable property which is pointed to our app router which is an instance of change notifier.

class AppRouter {
  // Class Properties - the changeNotifiers that represent
  // the state of our different screens.
  //1
  final AppStateManager appStateManager;
  // 2
  final ProfileManager profileManager;
  // 3
  final GroceryManager groceryManager;

  //Counstructor
  AppRouter(this.appStateManager, this.profileManager, this.groceryManager);

  // 4
  late final router = GoRouter(
    // 5
    debugLogDiagnostics: true,
    // 6
    refreshListenable: appStateManager, <- IT'S LISTENING HERE.
    //7
    initialLocation: '/login',
    // 8
    routes: [
      GoRoute(
        // /login
        name: 'login',
        path: '/login',
        builder: (context, state) => const LoginScreen(),
      ),
      GoRoute(
        name: 'onboarding',
        path: '/onboarding',
        builder: (context, state) => const OnboardingScreen(),
      ),

honestly couldn’t tell you about the first part of your question as I’m not that far along in the chapter yet.

1 Like

@kemmetdaniel
thank you for caring enough to say something!

Hi dev777,

The author for chapter 7 has had some personal issues and is currently not available, so others will need to cover for them. Your patience is appreciated.

For us to help you we need additional information. Chapter 7 does’t have anything about notifyListeners. Also, page numbers only if someone is looking at a print copy. It is more helpful to refer to a specific chapter, section, and if applicable sub-section. That way anyone reading the message knows where to find the materials being asked about; especially if the person reading is using the website as their reference. For example, your question regarding page 291, my copies don’t have have anything about notifyListeners() - not my print book (I don’t have Ed 3 yet in print) nor ePub.

Can you provide more details as to where you’re running into issues? That will help us help you.

Cheers,
Stef

Hi @spgms ,
In Edition 2, Chapter 7, section on “Adding State Listeners”, we have the following:

Locate // TODO: Add Listeners and replace it with the following:

appStateManager.addListener(notifyListeners);
groceryManager.addListener(notifyListeners);
profileManager.addListener(notifyListeners);

My 1st question pertained to that, and I’d still appreciate an answer on my 1st question.

Thank you to @kemmetdaniel for providing an answer to my 2nd question (Not sure yet if that’s the answer … I’d need to go back and read that section of the book again; however, it probably IS the answer, and I am very appreciative for the help!)