Sub topic 7
Deriving State by Composing Providers
Assume you know Dart classes, generics, async and await, and Flutter’s build method. This chapter also assumes that you can declare a provider and read it with ref.watch.
A derived provider calculates a value from another provider. It does not store a second copy of the source state. Instead, it describes a dependency:
When the source provider changes, Riverpod recomputes the derived provider. Widgets that watch the derived provider then rebuild if the derived value changed.
A derived provider in practice
This example displays a counter and whether its value is even or odd:
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
class Counter extends Notifier<int> {
@override
int build() => 0;
void increment() => state++;
}
final counterProvider = NotifierProvider<Counter, int>(Counter.new);
final parityProvider = Provider<String>((ref) {
final count = ref.watch(counterProvider);
return count.isEven ? 'Even' : 'Odd';
});
class CounterPage extends ConsumerWidget {
const CounterPage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final count = ref.watch(counterProvider);
final parity = ref.watch(parityProvider);
return Scaffold(
body: Center(
child: Text('$count ($parity)'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
ref.read(counterProvider.notifier).increment();
},
child: const Icon(Icons.add),
),
);
}
}
void main() {
runApp(
const ProviderScope(
child: MaterialApp(home: CounterPage()),
),
);
}parityProvider calls ref.watch(counterProvider). That call creates the dependency. Riverpod knows that it must recompute parityProvider after counterProvider changes.
The widget watches both providers because it displays both values. The button uses ref.read because it needs to call a method once. It does not need to rebuild the button when the count changes.
Composition and dependency boundaries
Providers can depend on other providers at several levels. For example, one provider can expose a repository, another can read the repository, and a third can filter the returned data. Each provider should perform one clear transformation.
| Provider role | Example responsibility | Usually watches |
|---|---|---|
| Source state | Store the current counter | Nothing or dependencies |
| Derived state | Calculate parity | `counterProvider` |
| Presentation state | Format text for a screen | Other derived providers |
This structure keeps calculation out of widget build methods. It also makes derived logic reusable and easier to test with a ProviderContainer.
Common errors
- Use
ref.watchinside a provider when the result should update after a dependency changes. - Use
ref.readfor one-time actions, such as calling a notifier method. - Avoid side effects inside a derived provider. A provider should calculate and return a value, not show a snackbar or change unrelated state.
- Remember that a derived provider can return any type, including a model, list, boolean, or formatted string.
Exercise
If a widget watches parityProvider and the counter changes from 2 to 4, should Riverpod recompute parityProvider?
Show answer
Yes. The provider watches counterProvider, so Riverpod recomputes it whenever the counter changes. The result remains Even, but the dependency is still evaluated.