Set up Riverpod for state management in Flutter
✓Works with OpenClaudeYou are a Flutter developer setting up Riverpod state management. The user wants to configure Riverpod providers, manage state lifecycles, and integrate it into their Flutter app.
What to check first
- Run
flutter pub list-pluginsto verify your Flutter environment is set up - Check your
pubspec.yamlto see ifriverpodandflutter_riverpodare already listed (they shouldn't be if starting fresh) - Ensure you're using Flutter 3.0+ with null safety enabled
Steps
- Add
flutter_riverpodandriverpodtopubspec.yamlunder dependencies, then runflutter pub get - Wrap your root
MaterialAppwithProviderScopeinmain()to initialize the Riverpod container - Define a
StateNotifierclass that extendsStateNotifier<T>to hold mutable state and methods - Create a
StateNotifierProviderthat instantiates yourStateNotifierto expose it across the widget tree - Use
ref.watch()inside widgets to subscribe to provider changes and rebuild automatically - Use
ref.read()when you need one-time access to a provider value (e.g., in button callbacks) - Use
ref.listen()to trigger side effects when a provider value changes without rebuilding - Test providers with
ProviderContainerin unit tests to verify state transitions
Code
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
// Define a StateNotifier to manage counter logic
class CounterNotifier extends StateNotifier<int> {
CounterNotifier() : super(0);
void increment() => state++;
void decrement() => state--;
void reset() => state = 0;
}
// Create a StateNotifierProvider to expose the counter
final counterProvider = StateNotifierProvider<CounterNotifier, int>((ref) {
return CounterNotifier();
});
// Example: Computed provider that depends on another provider
final isEvenProvider = Provider<bool>((ref) {
final count = ref.watch(counterProvider);
return count.isEven;
});
void main() {
runApp(
const ProviderScope(
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: const CounterScreen(),
title: 'Riverpod Example',
);
}
}
class CounterScreen extends ConsumerWidget {
const CounterScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
// Watch the counter provider — widget rebuilds when it changes
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Flutter Skills
Other Claude Code skills in the same category — free to download.
Flutter Widget
Build custom Flutter widgets with state management
Flutter Navigation
Configure GoRouter for declarative navigation
Flutter HTTP
Build HTTP client with Dio and interceptors
Flutter Firebase
Integrate Firebase (Auth, Firestore, Storage) in Flutter
Flutter Testing
Write widget tests and integration tests for Flutter
Flutter State Management with Riverpod
Manage app state in Flutter using Riverpod 2.x for type-safe reactive state
Flutter Platform Channels
Call native iOS and Android code from Flutter via platform channels
Want a Flutter skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.