Flutter › State Management with Provider
What is the difference between context.watch, context.read, and context.select, and how does correctly choosing the genuinely appropriate one help meaningfully avoid unnecessary, wasteful widget rebuilds?
IntermediateAnswer
context.watch subscribes the calling widget to future rebuilds whenever the given watched provider's own value actually genuinely changes, context.read simply reads the provider's own current value just once without establishing any kind of genuine ongoing subscription at all, making it genuinely appropriate specifically for use within a callback such as an onPressed handler, and context.select lets you subscribe to just one genuinely specific narrow piece of a given larger provided object, rebuilding your particular widget only when that one specific selected piece actually genuinely changes rather than rebuilding whenever any part of that entire larger object happens to change at all.
final itemCount = context.select<CartModel, int>((cart) => cart.items.length);Real-world example A cart badge widget properly uses context.select to specifically watch only the cart's own item count, avoiding an unnecessary rebuild whenever some other genuinely unrelated property on that exact same CartModel object happens to change instead.