BeginnerState Management

What is the Provider package and how does it work?

Short Answer

Provider is a wrapper around Flutter's InheritedWidget that makes it easy to expose and listen to a piece of state from anywhere below it in the widget tree, and to rebuild only the widgets that actually depend on it.

You wrap part of your widget tree in a `ChangeNotifierProvider` (or similar provider type), supplying a `ChangeNotifier` subclass that holds your state and calls `notifyListeners()` whenever it changes. Descendant widgets read that state with `context.watch<T>()` (rebuilds on change), `context.read<T>()` (reads once, doesn't rebuild — typically used inside callbacks), or the `Consumer<T>` widget for fine-grained rebuild scoping.

Provider's main value over raw InheritedWidget is dependency lookup and disposal handled for you, plus a clear, idiomatic API that the Flutter team itself recommended for years as the default state management approach for small-to-medium apps.

Common Mistakes

  • ×Calling `context.watch<T>()` inside a callback (like `onPressed`) instead of `context.read<T>()` — watch should only be used during build.
  • ×Putting business logic directly in widgets instead of in the ChangeNotifier.