Skip to content

skillMobileflutter

Flutter State Management

Chooses between Riverpod and Bloc for a Flutter feature, implements it cleanly and covers the logic and widgets with tests.

npx workspacetemplates add flutter-state-management --tool claude-code

Writes 1 file to .claude/skills/flutter-state-management/

.claude/skills/flutter-state-management/SKILL.md28 lines · 1.8 KBFile
---
name: flutter-state-management
description: Chooses between Riverpod and Bloc for a Flutter feature, implements it cleanly and covers the logic and widgets with tests.
---

# Flutter state management

Follow the project's existing choice. Introduce a second state library only if the user asks.

## Steps

1. Read `pubspec.yaml` and two existing features. Note which of `flutter_riverpod`, `flutter_bloc`, `provider` is in use and how files are laid out.
2. If nothing is in use, choose by need. Riverpod fits derived and cached async data, dependency injection and many small independent states. Bloc fits event-driven flows with strict transitions, such as multi-step forms, auth and payments, where an event log is valuable. Say which you chose and why in two sentences.
3. Model state as an immutable type with explicit cases: initial, loading, data, failure. Use a sealed class or freezed. Never a bag of nullable fields plus a boolean `isLoading`.
4. Riverpod: use `AsyncNotifier` or `Notifier`, read with `ref.watch` in `build`, use `ref.read` only in callbacks, and use `select` to narrow rebuilds. Bloc: one event class per user intent, one `on<Event>` handler each, emit only from handlers, and close subscriptions in `close()`.
5. Keep widgets thin. They render state and send events or call notifier methods. No repository calls, no `BuildContext` inside logic.
6. Tests: unit test the notifier with a `ProviderContainer` and overrides, or the bloc with `bloc_test`. Add widget tests that pump each state and check the visible result.
7. Run `dart format`, `flutter analyze` and `flutter test`.

## Rules

- Dispose controllers and cancel streams.
- Never call `notifyListeners` or emit after dispose.
- No global mutable singletons for state.

## Report

The library chosen and the reason, files added or changed, and test results.

What it does

State management in Flutter goes wrong when the choice is made by habit. This skill has the agent look at what the project already uses, what the feature actually needs, and only then pick Riverpod or Bloc and apply it in the same style as the rest of the code.

The result is a feature with a clear state type, logic that can be tested without a widget tree, and widget tests that pump real states instead of mocking everything.

Example

you › Add a favorites screen that loads from the API and lets users remove items. We use Riverpod already.

agent › Kept Riverpod. Added favorites_notifier.dart (AsyncNotifier<List<Favorite>>) with remove() doing an optimistic update and rollback on failure, favorites_screen.dart watching it with select for the count badge, and a repository override in tests. Tests: 5 notifier tests, 3 widget tests for loading, empty and error. flutter analyze clean, flutter test 8 passed.

Questions

Will it switch my app from Provider to Riverpod?
Not on its own. It works in the library you already use and suggests a migration plan only when you ask for one.
Why sealed classes instead of an isLoading flag?
A flag plus nullable data allows impossible combinations, such as loading with an error. Sealed cases make the compiler check every screen state.
  • Kotlin Android Compose

    Skillkotlin

    Free
  • Mobile App Release

    SkillMobile

    Free
  • Offline First Sync

    SkillMobile

    Free
  • React Native Performance

    Skillreact-native

    Free