D
State ManagementIntermediate30 XP2 min read

When should you use context.select() instead of context.watch() in Provider?

TL;DR: Use context.select() to subscribe to a specific field of a model. The widget only rebuilds when that field changes — avoiding rebuilds triggered by unrelated field updates in the same model.

Full Answer

context.watch() subscribes to the entire model — any notifyListeners() call triggers a rebuild. context.select() subscribes to a projection and only rebuilds when the projected value changes.

🎯

context.select() uses == equality. Ensure the projected value (e.g., a list) implements proper equality or use a primitive type to benefit from the optimization.

Code Examples

dartcontext.watch vs context.select
Output
After updateAge(31): watch widget rebuilds, select widget does NOT. After togglePremium(): both rebuild.

Common Mistakes

  • Using context.select returning a list — lists don't have value equality, so select always rebuilds
  • Using context.select for derived computations that are expensive — the selector runs on every notifyListeners

Interview Tip

💡

This is a performance micro-optimization question. Knowing it shows you've written large Provider-based apps where rebuild optimization matters.

#Provider#context.select#context.watch#rebuild-optimization