---
name: flutter-riverpod-state-management
description: 'Reactive state management using Riverpod 2.0 with code generation. Use when managing state with Riverpod providers or using riverpod_generator in Flutter. (triggers: **_provider.dart, **_notifier.dart, riverpod, ProviderScope, ConsumerWidget, Notifier, AsyncValue, ref.watch, @riverpod)'
---

# Riverpod State Management

## **Priority: P0 (CRITICAL)**

Type-safe, compile-time safe reactive state management using `riverpod` and `riverpod_generator`.

## Structure

```text
lib/
├── providers/ # Global providers and services
└── features/user/
    ├── providers/ # Feature-specific providers
    └── models/    # @freezed domain models
```

## Implementation Guidelines

- **Generator First**: Use **@riverpod** annotations and `riverpod_generator`. Avoid manual `Provider` definitions.
- **Immutability**: Maintain immutable states. Use `Freezed` for all state models.
- **Provider Methods**:
  - `ref.watch()`: Use inside `build()` to rebuild on changes. (e.g., `ref.watch(productsProvider)`)
  - **Side-Effects**: Use **ref.listen()** inside `build()` for navigation/dialogs (e.g., `ref.listen(cartProvider, (prev, next) { ... })` in `ConsumerWidget`). **Never perform side-effects inside provider initialization.**
  - `ref.read()`: Use ONLY in callbacks (`onPressed`).
- **Asynchronous Data**: Use **AsyncNotifier** for complex async logic. The `build()` method calls the repository (e.g., `repository.getProducts()`). Access data via **.when(data: , loading: , error: )** or `AsyncValue` pattern-matching.
- **Architecture**: Enforce 3-layer separation (Data, Domain, Presentation).
- **Testing**: Override providers in widget tests using **ProviderScope(overrides: [provider.overrideWithValue(Mock())])** in `pumpWidget`.
- **Linting**: Enable **riverpod_lint** and `custom_lint` for dependency cycle detection and to catch missing overrides.

## Anti-Patterns

- **Building Inside Providers**: Don't perform side-effects inside provider initialization.
- **Context Access**: Never pass `BuildContext` into a Notifier/Provider.
- **Dynamic Providers**: Avoid local provider instantiation; keep them global.

## Related Topics

layer-based-clean-architecture | dependency-injection | testing



---
Source attribution: HoangNguyen0403
Original project: https://github.com/HoangNguyen0403/agent-skills-standard
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Dave AI.

## Upstream license notice

MIT License

Copyright (c) 2026 SkillsHub Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Dave AI resource metadata

Creator / publisher: HoangNguyen0403
Original project: https://github.com/HoangNguyen0403/agent-skills-standard
Attribution basis: Original repository namespace preserved by package mirror

Fields of work: Development & Engineering, Product Management
Roles: Frontend Developer, Mobile Developer, Product Manager
Specialties: Frontend, Mobile
Tools: Flutter
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 97d0f7097ae72aa82065bcff821d2a056329bc640abfbca49322ab5e298467c7
Metadata reviewed: 2026-10-27
This metadata supersedes earlier Dave AI catalog attribution. Original instruction and supporting files are preserved.
