Auto Dispose
Safeguard against memory leaks and keep your state fresh using Riverpod AutoDispose.
Unlike classic Riverpod where you explicitly write .autoDispose, Riverpod Generator automatically applies auto-disposal to every generated provider by default.
The Default Behavior
Every provider marked with @riverpod immediately disposes its state when no longer actively watched by a widget or another provider.
@riverpod
Future<Cart> shoppingCart(ShoppingCartRef ref) async {
// Disposed automatically when user navigates away from cart screen!
return await fetchCart();
}Overriding Auto Dispose
To maintain state indefinitely (such as keeping app themes or authentication status in memory), use the keepAlive: true property:
@Riverpod(keepAlive: true)
String globalAppConfig(GlobalAppConfigRef ref) {
return 'Never Disposed';
}Benefits of Auto Dispose
- Memory safety: Pages holding heavy data chunks automatically clear from RAM when closed.
- Freshness: You don't have to worry about old cached data showing up next time you open a screen.
- Simplified debugging: You can track precisely what's currently active.