Sub topic 2
ProviderScope and the Provider Container
This chapter assumes you know Dart classes and generics, Flutter’s build method, and the difference between StatelessWidget and StatefulWidget. You should also understand that setState causes a widget to rebuild.
Riverpod moves provider state outside the widget tree. This separation lets several widgets use the same state without passing it through constructors. It also makes the state easier to test because it does not require a rendered Flutter screen.
What `ProviderScope` does
ProviderScope is a widget that creates a Riverpod container and makes that container available to its descendants. The container stores the current value of each provider, tracks dependencies, and notifies listeners when a value changes.
Put one ProviderScope near the root of the application:
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
final greetingProvider = Provider<String>((ref) {
return 'Hello from Riverpod';
});
void main() {
runApp(
const ProviderScope(
child: MyApp(),
),
);
}
class MyApp extends ConsumerWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final greeting = ref.watch(greetingProvider);
return MaterialApp(
home: Scaffold(
body: Center(
child: Text(greeting),
),
),
);
}
}ConsumerWidget receives a WidgetRef. The ref object connects the widget to the container. Calling ref.watch asks the container for the provider’s value and subscribes the widget to future changes.
Without ProviderScope, Riverpod cannot find a container for ref.watch. The application will fail when it tries to read the provider.
The container’s lifetime
A provider is usually created lazily. The container creates its value when a widget or another provider first reads it. The container then keeps that value according to the provider’s lifecycle rules.
The container also owns cleanup. When the ProviderScope is removed, its container is disposed, and providers can release resources such as stream subscriptions or controllers.
Nested scopes and overrides
You can place another ProviderScope below the root to override providers for one part of the widget tree. The nested scope receives its own container behavior while still accessing providers from its parent when they are not overridden.
This is useful for previews, feature-specific configuration, and tests. In production, a single root scope is often enough. Add nested scopes only when a subtree needs different dependencies.
ProviderScope versus ProviderContainer
Use ProviderScope in Flutter widget trees. Use ProviderContainer directly in tests or other non-widget Dart code.
A ProviderScope manages a container’s integration with Flutter. A ProviderContainer is the underlying object that stores provider state and performs provider evaluation.
Exercise
What is the main purpose of placing ProviderScope above the widgets that use Riverpod providers?
Show answer
It creates and exposes a provider container, allowing descendant widgets to read providers and receive updates.