evanca/flutter-ai-rules/bloc
Claude Code and agent skill by evanca/flutter-ai-rules
- community
Checking that the SKILL.md file is still in the repository.
Use when creating a Cubit or Bloc, modeling state with sealed classes or status enums, wiring BlocBuilder/BlocListener/BlocProvider, writing bloc tests, or choosing between Cubit and Bloc.
Install
npx vskill@latest install evanca/flutter-ai-rules/blocSKILL.md
Bloc Skill
Design, implement, and test state management using the bloc and flutter_bloc libraries.
When to Use
Use this skill when:
- Creating a new Cubit or Bloc for a feature.
- Modeling state (choosing between sealed classes and a single state class with status enum).
- Wiring
BlocBuilder,BlocListener,BlocConsumer, orBlocProviderin the widget tree. - Writing unit tests for a Cubit or Bloc.
- Deciding between Cubit and Bloc.
- Refactoring existing state management to follow bloc conventions.
1. Cubit vs Bloc
| Situation | Use | |
|---|---|---|
| Simple state, no events needed | Cubit | |
| Complex flows, event traceability needed | Bloc | |
| Advanced event processing (debounce, throttle) | Bloc with event transformers |
Default to `Cubit`. Refactor to `Bloc` only when requirements grow.
2. Naming Conventions
Events (Bloc only)
- Named in past tense:
LoginButtonPressed,UserProfileLoaded. - Format:
BlocSubject+ optional noun + verb. - Initial load event:
BlocSubjectStarted(e.g.,AuthenticationStarted). - Base event class:
BlocSubjectEvent.
States
- Named as nouns (states are snapshots in time).
- Base state class:
BlocSubjectState. - Sealed subclasses:
BlocSubject+Initial|InProgress|Success|Failure. - Example:
LoginInitial,LoginInProgress,LoginSuccess,LoginFailure. - Single-class approach:
BlocSubjectState+BlocSubjectStatusenum (initial,loading,success,failure).
3. Modeling State
When to use a sealed class with subclasses
- States are well-defined and mutually exclusive.
- Type-safe exhaustive
switchis desired. - Subclass-specific properties exist.
Handle all states exhaustively in the UI:
When to use a single class with a status enum
- Many shared properties across states.
- Simpler, more flexible; previous data must be retained after failure.
State rules (both approaches)
- Extend
Equatableand pass all relevant fields toprops. - Copy
List/Mapproperties withList.of/Map.ofinsideprops. - Annotate with
@immutable. - Always emit a new instance; never reuse the same state object.
- Duplicate states are ignored by bloc — ensure meaningful state changes.
4. Cubit Implementation
Rules:
- Only call
emitinside the Cubit/Bloc. - Public methods return
voidorFuture<void>only. - Keep business logic out of UI.
- When overriding
storagein aHydratedCubit, pass it as a named parameter:super(initialState, storage: storage).
5. Bloc Implementation
Rules:
- Trigger state changes via
bloc.add(Event()), not custom public methods. - Keep event handler methods private (
_onEventName). - Internal/repository events must be private and may use custom transformers.
6. Architecture
Three layers — each must stay in its own boundary:
- Data Layer: Repositories wrap data providers. Providers perform raw CRUD (HTTP, DB). Repositories expose clean domain objects.
- Business Logic Layer: Cubits/Blocs receive repository data and emit states. Inject repositories via constructor.
- Presentation Layer: Renders UI based on state. Handles user input by calling cubit methods or adding bloc events.
Rules:
- Blocs must not access data providers directly — only via repositories.
- No direct bloc-to-bloc communication. Use
BlocListenerin the UI to bridge blocs. - For shared data, inject the same repository into multiple blocs.
- When a repository must expose its latest value synchronously (for example
currentUser), write each update into a small injectable in-memory cache client and read from that cache, falling back to an empty value. Injecting the cache client keeps the repository testable. - Initialize
BlocObserverinmain.dart.
7. Flutter Bloc Widgets
| Widget | Use | |
|---|---|---|
BlocProvider | Provide a bloc to a subtree | |
MultiBlocProvider | Provide multiple blocs without nesting | |
BlocBuilder | Rebuild UI on state change | |
BlocListener | Side effects only (navigation, dialogs, snackbars) | |
MultiBlocListener | Listen to multiple blocs without nesting | |
BlocConsumer | Rebuild UI + side effects together | |
BlocSelector | Rebuild only when a selected slice of state changes | |
RepositoryProvider | Provide a repository to the widget tree | |
MultiRepositoryProvider | Provide multiple repositories without nesting |
Rules:
- Use
context.read<T>()in callbacks (not inbuild). - Use
context.watch<T>()inbuildonly when necessary; preferBlocBuilder. - Never call
context.watchorcontext.selectat the root ofbuild— scope withBuilder. - Handle all possible states in the UI (initial, loading, success, failure).
8. Testing
Use bloc_test package. Mock repositories with mocktail.
Rules:
- Always call
tearDown(() => cubit.close()). - Use
blocTestfor state emission assertions. - Use
group()named after the class under test. - Name test cases with "should" to describe expected behavior.
- Register fallback values for custom types:
registerFallbackValue(MyEvent()).
9. Common Pitfalls
| Pitfall | Fix | |
|---|---|---|
| Emitting the same state instance twice | Always create a new state object; bloc ignores duplicate emissions via ==. | |
Calling context.watch inside callbacks | Use context.read in callbacks; watch is only valid inside build. | |
Forgetting Equatable props | Add every field to props; missing fields cause silent state update bugs. | |
| Mutable state fields | Keep state @immutable; use copyWith or new sealed subclass instances. | |
| Business logic in widgets | Move all logic into the Cubit/Bloc; widgets only dispatch events or call methods. |
References
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/bloc/SKILL.md
Popularity
- Repository stars
- 643
- Repository forks
- 66
- 7-day trend score
- 32
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 21 Mar 2026, 7 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/evanca/flutter-ai-rules/bloc)