Chapter 7 Stateful Widget Errata

Quoting from the book:

In lib/main.dart, Fooderlich is now a StatefulWidget. It’ll listen to state changes
and rebuild corresponding widgets accordingly

So in the book, Fooderlich is changed to a StatefulWidget in this chapter so “it’ll listen to state changes and rebuild…” as the authors claim. But in reality, why would it need to be a StatefulWidget? Consumer is used, so it seems to work perfectly as a Stateless Widget like this:

class Fooderlich extends StatelessWidget {
  Fooderlich({
    super.key,
    required this.appStateManager,
  });

  final AppStateManager appStateManager;
  late final _groceryManager = GroceryManager();
  late final _profileManager = ProfileManager();
  late final _appRouter = AppRouter(
    appStateManager,
    _profileManager,
    _groceryManager,
  );

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => _groceryManager,
        ),
        ChangeNotifierProvider(
          create: (context) => _profileManager,
        ),
        ChangeNotifierProvider(
          create: (context) => appStateManager,
        ),
      ],
      child: Consumer<ProfileManager>(
        builder: (context, profileManager, child) {
          ThemeData theme;
          if (profileManager.darkMode) {
            theme = FooderlichTheme.dark();
          } else {
            theme = FooderlichTheme.light();
          }

          final router = _appRouter.router;

          return MaterialApp.router(
            theme: theme,
            title: 'Fooderlich',
            routerDelegate: router.routerDelegate,
            routeInformationParser: router.routeInformationParser,
            routeInformationProvider: router.routeInformationProvider,
          );
        },
      ),
    );
  }
}

Feel free to try, the app seems to work perfectly like that, which to me makes sense as Consumer takes care of the state changes. When Consumer is used, you don’t need to change this to a Stateful widget.

This same thing can also be seen in Chapter 6, where the class Home is a Stateful Widget, but it does not need to be…

Please advise,
Márton