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.
Related Questions
How does Riverpod differ from Provider?
Riverpod is a redesign by the same author as Provider that removes the dependency on BuildContext and the widget tree, catching errors at compile time instead of runtime, and making providers globally accessible and easily testable.
What is the BLoC pattern in Flutter?
BLoC (Business Logic Component) separates UI from business logic by having the UI dispatch Events into a Bloc, which processes them and emits new States that the UI rebuilds from — all communication happens through a strict, unidirectional stream of Events and States.