Back to results for “Dependency injection”
Flutter › State Management with Riverpod

What makes Riverpod genuinely different from Provider in terms of letting you properly read a given provider's own current value from essentially anywhere, entirely outside of the actual widget tree itself?

Beginner

Answer

Unlike Provider, which genuinely requires a valid BuildContext to actually properly read a given provided value, meaning you can only genuinely access it from directly within a widget, Riverpod's own providers genuinely exist entirely independently of the actual widget tree, letting you safely read a given provider's own current value from essentially anywhere at all, including from directly within a completely separate other provider, a background service, or even a unit test, without ever needing a genuine BuildContext at all, which represents one of Riverpod's own most genuinely significant particular improvements over the earlier Provider package.
final container = ProviderContainer();
final value = container.read(myProvider);
Real-world example A background sync service reads a shared configuration value directly from a Riverpod provider without needing any genuine BuildContext at all, something that would have simply not actually been possible using Provider's own considerably more restrictive context based access approach.

Common follow-ups: How does Riverpod genuinely still let a widget properly react to a given provider's own value changing over time?;What genuinely is a ProviderContainer, and precisely when would you actually genuinely need to directly interact with one yourself?