What must wrap your app to use Riverpod?
ProviderScope stores all provider state and must wrap the entire app at the root.
Modern, compile-safe state management with Riverpod
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Riverpod is a reactive caching and data-binding framework for Flutter. It is the evolution of the Provider package, designed to be fully compile-safe, highly testable, and completely independent of BuildContext. Unlike Provider, Riverpod catches errors at compile time rather than at runtime, supports multiple providers of the same type, and works outside the widget tree. It handles asynchronous data with built-in loading and error states, making it ideal for apps that fetch data from APIs, databases, or other async sources. Riverpod 3.x also supports code generation with the @riverpod annotation for even cleaner syntax.
1// pubspec.yaml: flutter_riverpod: ^2.4.02import 'package:flutter_riverpod/flutter_riverpod.dart';34// ── Step 1: Wrap app with ProviderScope ──5void main() {6runApp(const ProviderScope(child: MyApp()));7}89// ── Simple StateProvider ──10final counterProvider = StateProvider<int>((ref) => 0);1112// ── StateNotifierProvider (for complex state) ──13class TodoNotifier extends StateNotifier<List<String>> {14TodoNotifier() : super([]);1516void add(String todo) => state = [...state, todo];17void remove(int index) {18state = [...state]..removeAt(index);19}20}2122final todoProvider = StateNotifierProvider<TodoNotifier, List<String>>(23(ref) => TodoNotifier(),24);2526// ── FutureProvider (for async data) ──27final userProvider = FutureProvider<User>((ref) async {28final response = await http.get(Uri.parse('https://api.example.com/user'));29return User.fromJson(jsonDecode(response.body));30});3132// ── Using in widgets ──33class CounterPage extends ConsumerWidget {34@override35Widget build(BuildContext context, WidgetRef ref) {36final count = ref.watch(counterProvider);3738return Scaffold(39body: Center(child: Text('Count: $count')),40floatingActionButton: FloatingActionButton(41onPressed: () => ref.read(counterProvider.notifier).state++,42child: const Icon(Icons.add),43),44);45}46}
Use ref.watch() inside the build() method to subscribe to a provider and rebuild the widget whenever the value changes. Use ref.read() inside callbacks like onPressed or onTap to read a value once without subscribing to future changes. Using ref.read() in build() is a common mistake that prevents your widget from updating when state changes.
Use Riverpod as a dependency graph, not just a state container:
This model keeps your app modular and makes unit tests straightforward without widget scaffolding.
In modern Flutter development, the Riverpod community has standardized on Code Generation using @riverpod annotations and build_runner. This eliminates manual boilerplate providers like StateNotifierProvider or ChangeNotifierProvider.
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'counter_notifier.g.dart';
// Generates a type-safe AutoDisposeNotifierProvider:
@riverpod
class CounterNotifier extends _\$CounterNotifier {
@override
int build() => 0; // Initial state
void increment() => state++;
}
// In your UI widget:
class CounterScreen extends ConsumerWidget {
const CounterScreen({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final count = ref.watch(counterNotifierProvider);
return Scaffold(
body: Center(child: Text('Count: \$count')),
floatingActionButton: FloatingActionButton(
onPressed: () => ref.read(counterNotifierProvider.notifier).increment(),
child: const Icon(Icons.add),
),
);
}
}
AsyncValue.Modern, compile safe state management with Riverpod. State is data that changes over time and controls what users see on the screen.
Think in architecture, reliability, and team-level maintainability.
Keep state as close as possible to where it is used, then lift it only when sharing is required.
State management is the difference between predictable apps and hard-to-debug UI behavior.
State boundaries define module boundaries, so clear ownership prevents future regressions.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
ProviderScope stores all provider state and must wrap the entire app at the root.
ref.watch() in build() makes the widget rebuild on changes. ref.read() in callbacks reads once without rebuilding.
FutureProvider automatically handles loading/error/data states for async operations.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue