Common questions and answers about Riverpod Gen Shortcuts, code generation, and best practices.
No, code generation is optional. You can use Riverpod with manual provider definitions. However, code generation provides several benefits:
Yes. You can use both generated providers with @riverpod and manual providers in the same project. This allows gradual migration or mixing approaches based on your needs.
For development:
dart run build_runner watchFor one-time builds:
dart run build_runner buildTo clean generated files:
dart run build_runner cleanCheck these common issues:
Make sure your part directive matches the file name exactly. For a file named counter.dart, use:
part 'counter.g.dart';The generated file will be counter.g.dart and should contain:
part of 'counter.dart';Limited customization is available through annotations. You can:
Follow these best practices:
Use family providers when you need multiple instances of the same provider with different parameters:
@riverpod
Future<User> user(UserRef ref, int userId) async {
return await userRepository.getUser(userId);
}
// Usage: ref.watch(userProvider(123))Generated providers handle disposal automatically. For manual control:
@Riverpod(keepAlive: false) // Auto-dispose (default)
@Riverpod(keepAlive: true) // Keep alive
// Or use ref.keepAlive() conditionally:
@riverpod
String expensiveComputation(ExpensiveComputationRef ref) {
ref.keepAlive(); // Prevent disposal
return performExpensiveOperation();
}Use ProviderContainer for unit testing:
test('counter increments correctly', () {
final container = ProviderContainer();
expect(container.read(counterProvider), 0);
container.read(counterProvider.notifier).increment();
expect(container.read(counterProvider), 1);
});Yes, use overrides:
final container = ProviderContainer(
overrides: [
userRepositoryProvider.overrideWithValue(mockUserRepository),
],
);Follow these migration steps:
Yes, but they use different widget trees. You will need separate ProviderScope and MultiProvider widgets until migration is complete.
We provide snippets and extensions for:
Make sure to:
Can't find what you're looking for? Join our GitHub discussions or open an issue. Our community is here to help.