fix: theme toggle causing auth issues due to auto-dispose
- Add @persistentRiverpod annotation for providers that need keepAlive - ThemeProvider now persists for app lifetime - Refactored API clients to use @persistentRiverpod - Documented in ARCHITECTURE.md 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.5
parent
c617d7dfbb
commit
265ca5959d
@@ -420,6 +420,41 @@ ContainerRepository containerRepository(Ref ref) {
|
||||
}
|
||||
```
|
||||
|
||||
### Persistent Providers
|
||||
|
||||
By default, `@riverpod` generates providers with `isAutoDispose: true`, meaning they dispose when no longer watched. This causes issues for:
|
||||
|
||||
- **API clients** with interceptors that store a `Ref`
|
||||
- **App-level state** like theme, auth, config
|
||||
- **Providers with listeners** to other providers
|
||||
|
||||
Use `@persistentRiverpod` from `core/providers/annotations.dart` for these cases:
|
||||
|
||||
```dart
|
||||
import 'package:tatlock_ui/core/providers/annotations.dart';
|
||||
|
||||
// ✅ Correct - persists for app lifetime
|
||||
@persistentRiverpod
|
||||
Dio coreApiClient(Ref ref) { ... }
|
||||
|
||||
@persistentRiverpod
|
||||
class ThemeNotifier extends _$ThemeNotifier { ... }
|
||||
|
||||
// ❌ Wrong - auto-dispose can invalidate stored Ref
|
||||
@riverpod
|
||||
Dio coreApiClient(Ref ref) { ... }
|
||||
```
|
||||
|
||||
**When to use `@persistentRiverpod`:**
|
||||
|
||||
| Use Case | Annotation |
|
||||
|----------|------------|
|
||||
| API clients with interceptors | `@persistentRiverpod` |
|
||||
| Theme/config providers | `@persistentRiverpod` |
|
||||
| Auth state provider | `@persistentRiverpod` |
|
||||
| Feature data providers | `@riverpod` (default) |
|
||||
| UI state providers | `@riverpod` (default) |
|
||||
|
||||
## File Naming Conventions
|
||||
|
||||
| Type | Convention | Example |
|
||||
|
||||
Reference in New Issue
Block a user