flutter-state-management
/flutter-state-management
State management in Flutter with strict immutability and lifted state — lifting every meaningful value (selection, filters, form drafts, expanded flags, pagination) out of widgets into an owning notifier/bloc/view model scoped to app, feature, or route; deeply immutable state (freezed primary-constructor classes, IList/ISet/IMap from fast_immutable_collections, @immutable); pure state transitions; value equality; sealed loading/data/error states; self-connected components vs rows/sections that take values; narrow subscriptions to avoid rebuild storms; choosing Riverpod, Bloc, or ValueNotifier
- Author
- Dee9975 ↗
- Repository
- cc-skills-flutter ↗
- Licence
- MIT
- Last repository change
- 05.11.2026
Assessment and security
- Files in this package
- 1
- Scripts
- 0
- HTML comments
- 0
- URLs in SKILL.md
- 0
No signals detected. Repository changed on 05.11.2026, without references to retired models or APIs.
MIT, open: use and modify while retaining author attribution.
What it does and when to use it
State management in Flutter with strict immutability and lifted state — lifting every meaningful value (selection, filters, form drafts, expanded flags, pagination) out of widgets into an owning notifier/bloc/view model scoped to app, feature, or route; deeply immutable state (freezed primary-constructor classes, IList/ISet/IMap from fast_immutable_collections, @immutable); pure state transitions; value equality; sealed loading/data/error states; self-connected components vs rows/sections that take values; narrow subscriptions to avoid rebuild storms; choosing Riverpod, Bloc, or ValueNotifier
Original author description
State management in Flutter with strict immutability and lifted state — lifting every meaningful value (selection, filters, form drafts, expanded flags, pagination) out of widgets into an owning notifier/bloc/view model scoped to app, feature, or route; deeply immutable state (freezed primary-constructor classes, IList/ISet/IMap from fast_immutable_collections, @immutable); pure state transitions; value equality; sealed loading/data/error states; self-connected components vs rows/sections that take values; narrow subscriptions to avoid rebuild storms; choosing Riverpod, Bloc, or ValueNotifier
How it works
- Lift state up
- Deeply immutable state
- Pure transitions
- Picking a solution
- Minimal MVVM with an immutable value
- Where widgets read state
- Where logic lives
- Review checklist
SKILL.md preview
---
name: flutter-state-management
description: "State management in Flutter with strict immutability and lifted state — lifting every meaningful value (selection, filters, form drafts, expanded flags, pagination) out of widgets into an owning notifier/bloc/view model scoped to app, feature, or route; deeply immutable state (freezed primary-constructor classes, IList/ISet/IMap from fast_immutable_collections, @immutable); pure state transitions; value equality; sealed loading/data/error states; self-connected components vs rows/sections that take values; narrow subscriptions to avoid rebuild storms; choosing Riverpod, Bloc, or ValueNotifier MVVM. Use when designing state, choosing a library, reviewing state shape, or refactoring setState-heavy screens. For library APIs, load flutter-riverpod or flutter-bloc."
license: MIT
metadata:
author: Matiss Kalnins
version: "0.5.0"
---
**Persona:** You lift state as high as it's meaningful, make it deeply immutable, and give each piece exactly one owner. Widgets render it deterministically. The library matters less than this discipline, so follow whatever the project already uses.
## Lift state up
Every value that affects what the user sees, or that would be missed if lost, lives in the state layer, not in a widget's `State`.
| Scope | Examples | Owner |
| --- | --- | --- |
| App | Auth session, settings, theme mode, locale, cart | App-scoped provider/bloc (keepAlive) |
| Feature / route | Fetched data, filters, sort, selected tab, expanded items, pagination, form drafts, validation errors | Feature- or route-scoped notifier/bloc/view model |
| Framework internals | Text selection inside a controller, scroll offset, animation ticks, ink ripples | Controller leaf widgets, mirroring lifted values (see `flutter-widgets`) |
- There's no "it's just UI state" exemption. Selected tabs, expanded rows, and text drafts are lifted, because that's what makes widgets deterministic, testable, and restorable.
- Scope state to the **highest level where it means something**, and no higher. Route-level state is disposed with the route (autoDispose providers, `BlocProvider` on the route). Lifting doesn't mean making everything global.
- When two widgets need the same value, it already belongs to their common owner in the state layer. Never sync two copies.
## Deeply immutable state
```dart
@freezed
class const CartState({ // primary constructor (see dart-json-serialization)
final IList<Item> items = const IListConst([]), // IList: immutable, value equality
final ISet<String> expandedIds = const ISetConst({}),
final String? couponCode,
final AppFailure? syncFailure, // last failed save, shown by the UI
}) with _$CartState {
int get total => items.fold(0, (sum, i) => sum + i.price); // derived, not stored
CartState add(Item item) => copyWith(items: items.add(item));
CartState remove(String id) => copyWith(items: items.removeWhere((i) => i.id == id));
CartState toggleExpanded(String id) => copyWith(
expandedIds: switch (expandedIds.contains(id)) {
true => expandedIds.remove(id),
false => expandedIds.add(id),
},
);
}
```
- **Every field is `final`, and every class is immutable.** Annotate hand-written state with `@immutable` (`package:meta`) and give it `const` constructors.
- **Collections are `IList` / `ISet` / `IMap`** from `fast_immutable_collections`, never `List`/`Set`/`Map`. A `final List` can still be mutated, and freezed primary-constructor classes don't wrap collections for you. The `I*` types have value equality, and every change returns a new instance.
- **Value equality is required** (freezed, records, `equatable`), so unchanged state doesn't notify, and tests can compare states directly.
- **Change state by replacing it**, through `copyWith` and the `I*` collection methods (`items.add(x)` returns a new `IList`). Never mutate it in place and then notify.
- **No impossible states.** Use a sealed hierarchy, or `AsyncValue`, instead of `isLoading` + `error` + nullable data.
- **Derive, don't duplicate**: computed getters (`total`, `isValid`, `emailError`) instead of extra stored fields.
## Pure transitions
State changes are pure methods on the state class itself (`add`, `remove`, `toggleExpanded` above), not free functions or extensions. The notifier or bloc stays a thin shell that calls them.
Pure transitions are deterministic and can be unit-tested with no mocks: `expect(CartState().add(a).add(b).total, 500)`.
## Picking a solution
1. **Already in `pubspec.yaml`?** Use it. Don't mix two libraries for the same job.
2. **New project**:
- **Riverpod**: scoped, overridable owners, and `AsyncValue`. **The default for this bundle's feature-based architecture**, where each feature has a `providers/` folder (see `flutter-project-layout`). → `flutter-riverpod`
- **Bloc/Cubit**: explicit events → immutable states, with strong conventions. → `flutter-bloc`
- **`ValueNotifier<ImmutableState>` + `ListenableBuilder`**: no dependencies, and matches the Flutter team's MVVM guide.
3. Avoid GetX for new work. Its mutable observables and hidden global state work against everything here.
## Minimal MVVM with an immutable value
```dart
class CartViewModel extends ValueNotifier<CartState> {
CartViewModel(this._repo) : super(const CartState());
final CartRepository _repo;
Future<void> add(Item item) async {
value = value.add(item); // replace, never mutate
value = switch (await _repo.save(value.items)) { // repositories return Result
Ok() => value.copyWith(syncFailure: null),
Err(:final failure) => value.copyWith(syncFailure: failure),
};
}
}
```
The view model holds no mutable fields other than its single `value`.
## Where widgets read state
- **Self-connected components** (lists, toggles, chips) watch the provider they display and call its notifier. A screen composes them, so the screen itself usually doesn't watch anything.
- **Rows and sections** take values and callbacks from the component that renders them.
- Subscriptions stay narrow: a component watches the one provider it shows, and derived providers do the grouping and sorting, so a change rebuilds only that component (see `flutter-widgets`, `flutter-riverpod`).
## Where logic lives
- **Widgets**: render state and forward intents to notifiers. No business rules, no I/O, no meaningful mutable state.
- **Notifier / Bloc / View model**: the single owner. It applies pure transitions, calls repositories, and maps results into state.
- **Repository**: data access and caching. It returns immutable models wrapped in `Result`.
- Side effects (snackbars, navigation) run in listeners (`ref.listen`, `BlocListener`), never in `build`.
## Review checklist
- [ ] No meaningful values in widget `State` or `setState`; everything lifted to an owner
- [ ] Each piece of state has exactly one owner, scoped as high as it's meaningful
- [ ] State classes are freezed primary-constructor classes (or `@immutable`), with `IList`/`ISet`/`IMap` collections and value equality
- [ ] No in-place mutation; transitions are pure and unit-tested
- [ ] Sealed or `AsyncValue` states; no boolean-flag combinations
- [ ] Widgets read state only through providers; each component watches the narrowest provider it needs
Versions
| Repository | Score | Licence |
|---|---|---|
| This page · Dee9975/cc-skills-flutter | 100 | MIT |
| Albaylar/Mobile-Development-Kit | 90 | MIT |
| thiennc-tesoglobal/flutter-skills | 90 | BSD-3-Clause |
Database snapshot: 13.11.2026 · package version 45c1b97425a5 (SHA-256 digest of the entire package)
Downloads in the last 30 days: 2 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 90flutter-core:flutter-state-management
Comprehensive Flutter state management guidance covering built-in solutions, Provider, BLoC, and Riverpod. Use when working with state management, sharing data between widgets, managing app state, implementing reactive patterns, choosing state solutions, setState, ValueNotifier,… - 100flutter-architecture
Provides Flutter app architecture patterns with feature-first structure, clean architecture, and state management (Bloc, Riverpod, Provider). Use when user asks about Flutter project structure, state management selection, dependency injection, or designing Flutter app architecture. - 100flutter-state-management-bloc-riverpod
Use this skill when implementing decoupled, reactive mobile state management in Flutter using BLoC event streams and Riverpod providers. - 95dart-flutter-patterns
Production-ready Dart and Flutter patterns covering null safety, immutable state with Freezed, async composition, widget architecture, state management (BLoC, Riverpod, Provider), GoRouter navigation with auth guards, Dio networking, error handling, and testing. Use when writing… - 100flutter-bloc-state-management
Implement BLoC/Cubit state, events, transitions, and async concurrency in Flutter. Use for BLoC/Cubit feature logic, debounced/cancellable events, state rendering, or bloc tests—not generic widget-only work. - 95flutter-state-management-and-widget-design
Production-grade Flutter state management (Riverpod 2.x, BLoC/Cubit), widget tree optimization, immutable state modeling, declarative routing (GoRouter), clean architecture, and custom rendering performance patterns. Use when architecting or building scalable Flutter applications.