Sub topic 11
Scoped Providers for Subtrees Such as List Items
This chapter assumes Dart async/await, Futures and Streams, basic Flutter widgets, and basic Riverpod usage with ProviderScope, ConsumerWidget, and ref.watch. It also assumes that riverpod_generator and build_runner are already configured.
A normal provider supplies one value to every widget below the application’s ProviderScope. A scoped provider supplies a different value to a particular subtree. This is useful when the same widget type appears many times, such as a list of item tiles. Each tile can read “the current item” without receiving that item through several widget constructors.
The scoped-provider pattern
The provider that represents the current item is declared as a dependency-injection point. Its default implementation throws because the application must override it inside a subtree.
import 'package:flutter/foundation.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'item_scope.g.dart';
@immutable
class Item {
const Item({required this.id, required this.title});
final int id;
final String title;
}
@Riverpod(dependencies: [])
Item currentItem(Ref ref) {
throw UnimplementedError(
'Override currentItemProvider in a ProviderScope',
);
}
@Riverpod(dependencies: [currentItem])
String currentItemTitle(Ref ref) {
return ref.watch(currentItemProvider).title;
}The empty dependencies list on currentItem tells Riverpod that this provider is intended to be scoped. The currentItemTitle provider declares that it depends on currentItem. This declaration lets Riverpod resolve the scoped value correctly when the title provider is read below an override.
The generator creates currentItemProvider and currentItemTitleProvider. The generated providers are normal Riverpod providers, so widgets can watch them through ref.watch.
Overriding one value for each list item
Place a ProviderScope around each item’s subtree. Its overrides list replaces currentItemProvider only for that subtree.
class ItemList extends StatelessWidget {
const ItemList({required this.items, super.key});
final List<Item> items;
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
final item = items[index];
return ProviderScope(
overrides: [
currentItemProvider.overrideWith((ref) => item),
],
child: const ItemTile(),
);
},
);
}
}
class ItemTile extends ConsumerWidget {
const ItemTile({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final title = ref.watch(currentItemTitleProvider);
return ListTile(
title: Text(title),
);
}
}For the first tile, currentItemProvider returns the first Item. For the second tile, the same provider returns the second Item. The ItemTile class does not need an item field. Its provider reads the value from the nearest scope.
Why the dependencies list matters
Riverpod normally builds a provider from the nearest available dependencies. A scoped provider changes that resolution for descendants. Any generated provider that reads the scoped provider should list it in dependencies.
@Riverpod(dependencies: [currentItem])
String currentItemId(Ref ref) {
return ref.watch(currentItemProvider).id.toString();
}Without the declaration, the provider graph does not clearly describe that currentItemId can change when a scope overrides currentItemProvider. The provider may fail during generation, fail at runtime, or behave as though it belongs to an outer scope, depending on the Riverpod version and provider configuration. Treat the dependencies list as part of the provider’s contract.
A scoped provider should usually have a small purpose. It can represent a route parameter, a selected document, a list item, or another value that changes with the location in the widget tree. Keep repositories and application-wide services as ordinary providers unless they genuinely need subtree-specific implementations.
Common mistakes
- Omitting
dependencies: []from the provider intended for overriding. - Declaring
currentItem, but forgetting to list it in the dependencies of providers that watch it. - Placing the
ProviderScopeabove the entire list when every row should have a different value. - Reusing a mutable
Itemobject and expecting Riverpod to detect changes inside its fields. Replace the object or expose changing fields through another provider. - Forgetting to run
dart run build_runner buildor watch mode after changing annotations.
Exercise
Where should the ProviderScope override go if every row in a ListView must expose a different Item to its ItemTile child?
Show answer
Inside the itemBuilder, around that row's ItemTile, with currentItemProvider.overrideWith returning the row's Item.