Frequently Asked Questions

Common questions and answers about Riverpod Gen Shortcuts, code generation, and best practices.

Getting Started

Do I need to use code generation with Riverpod?

No, code generation is optional. You can use Riverpod with manual provider definitions. However, code generation provides several benefits:

  • Automatic disposal and lifecycle management
  • Type-safe provider references
  • Reduced boilerplate code
  • Better IDE support and refactoring
Can I mix generated and manual providers?

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.

Which build_runner command should I use?

For development:

terminal
dart run build_runner watch

For one-time builds:

terminal
dart run build_runner build

To clean generated files:

terminal
dart run build_runner clean

Common Issues

Build runner is not generating files. What is wrong?

Check these common issues:

  • Make sure you have the part directive in your file
  • Verify riverpod_generator is in dev_dependencies
  • Check for syntax errors in your provider code
  • Run flutter clean and try again
  • Ensure file names match the part directive
I get part of errors. How do I fix them?

Make sure your part directive matches the file name exactly. For a file named counter.dart, use:

counter.dart
part 'counter.g.dart';

The generated file will be counter.g.dart and should contain:

counter.g.dart
part of 'counter.dart';
Can I customize the generated code?

Limited customization is available through annotations. You can:

  • Use @Riverpod(keepAlive: true) to prevent disposal
  • Add dependencies with the dependencies parameter
  • Control caching behavior with provider modifiers

Performance

How do I optimize provider performance?

Follow these best practices:

  • Use select to watch only specific parts of state
  • Implement efficient == operators for custom state classes
  • Use keepAlive for expensive computations
  • Avoid creating providers inside build methods
  • Use family providers for parameterized data
When should I use family providers?

Use family providers when you need multiple instances of the same provider with different parameters:

user_provider.dart
@riverpod
Future<User> user(UserRef ref, int userId) async {
  return await userRepository.getUser(userId);
}

// Usage: ref.watch(userProvider(123))
How do I handle provider disposal?

Generated providers handle disposal automatically. For manual control:

disposal.dart
@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();
}

Testing

How do I test providers?

Use ProviderContainer for unit testing:

counter_test.dart
test('counter increments correctly', () {
  final container = ProviderContainer();
  
  expect(container.read(counterProvider), 0);
  
  container.read(counterProvider.notifier).increment();
  
  expect(container.read(counterProvider), 1);
});
Can I mock providers for testing?

Yes, use overrides:

provider_override.dart
final container = ProviderContainer(
  overrides: [
    userRepositoryProvider.overrideWithValue(mockUserRepository),
  ],
);

Migration

How do I migrate from Provider to Riverpod?

Follow these migration steps:

  1. 1Replace ChangeNotifierProvider with NotifierProvider
  2. 2Update Consumer widgets to ConsumerWidget
  3. 3Change context.read() to ref.read()
  4. 4Replace context.watch() with ref.watch()
  5. 5Gradually adopt code generation for new providers
Can I use Riverpod with existing Provider code?

Yes, but they use different widget trees. You will need separate ProviderScope and MultiProvider widgets until migration is complete.

IDE Support

Which IDEs support Riverpod snippets?

We provide snippets and extensions for:

  • Visual Studio Code (Riverpod Snippets extension)
  • IntelliJ IDEA / Android Studio (Live Templates)
  • Vim/Neovim (with LSP support)
How do I get better autocomplete for providers?

Make sure to:

  • Run build_runner to generate provider files
  • Restart your IDE after generating code
  • Use dart analyze to check for issues
  • Enable Dart/Flutter language server features

Still have questions?

Can't find what you're looking for? Join our GitHub discussions or open an issue. Our community is here to help.