Sub topic 1
Why Riverpod: moving state out of the widget tree
Prerequisites
This chapter assumes you know Dart classes, generics, Future, and async/await. You should also understand Flutter widgets, the build method, StatefulWidget, and setState. The examples use Riverpod 2.x with Flutter.
The problem with widget-owned state
Flutter’s widget tree describes the current user interface. A widget can also own state with StatefulWidget and setState, which works well for local concerns such as whether a password is visible or which tab is selected.
Application state has a longer life and a wider audience. Examples include the signed-in user, a shopping cart, cached server data, and the selected workspace. If this state lives inside one widget, other widgets must receive it through constructor parameters or callbacks. This is called prop drilling: passing data through widgets that do not use it themselves.
Prop drilling becomes difficult when:
- Several distant widgets need the same value.
- A value must survive navigation between screens.
- Business rules become mixed with presentation code.
- Tests need to exercise state without building a complete widget tree.
Riverpod addresses this by placing state in providers outside the widget tree. Widgets still display the state, but they do not have to own it.
A separate state layer
A provider is a declaration that describes how to create or derive a value. Riverpod tracks which providers depend on each other and which widgets listen to them. A ProviderContainer stores the provider instances and their current values.
In a Flutter application, ProviderScope creates the container and makes it available to descendant widgets:
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
void main() {
runApp(
const ProviderScope(
child: MaterialApp(home: HomePage()),
),
);
}
class HomePage extends ConsumerWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
return const Scaffold(
body: Center(child: Text('State is managed by Riverpod')),
);
}
}ProviderScope belongs near the root because it gives the whole application access to the same container. A ConsumerWidget is a widget that receives a WidgetRef. The WidgetRef is the widget’s connection to Riverpod. Later chapters use it to watch values, read them for one-time actions, and listen for side effects.
The widget tree still controls layout and screen lifetime. Riverpod controls provider state and dependency relationships. This separation means that rebuilding a widget does not automatically recreate all application state.
Choosing the boundary
Keep purely visual, temporary state in a widget. Move state to Riverpod when multiple parts of the application need it, when it represents business data, or when it must be tested independently from Flutter rendering.
Exercise
Where should the state for a text field’s temporary focus highlight usually live, and where might the state for a signed-in user live?
Show answer
The focus highlight is local UI state and can live in the widget. The signed-in user is shared application state and is a good candidate for a Riverpod provider.