implementing-flutter-bloc
Skill7 starsChanged 8 months agoMITView on GitHub
Implements BLoC (v9.x) event-driven state management for Flutter with unidirectional data flow using the flutter_bloc package. Use when implementing Bloc/Cubit classes, mapping Events to States, consuming state with BlocBuilder/BlocListener/BlocConsumer, applying event transformers (restartable/droppable/sequential) for concurrency control, setting up BlocProvider/MultiBlocProvider DI, integrating Freezed for sealed union types and exhaustive pattern matching, or testing with blocTest. Ideal for enterprise apps requiring business logic separation, precise concurrency control (search debouncing, form deduplication), or event replay debugging.
In this repository41 other files it gives its agents.
- .github/copilot-instructions.mdCopilot instructions
- skill-creatorSkill
- flutter-dbSkill
- flutter-routingSkill
- flutter-state-managementSkill
- configuring-codemagicSkill
- applying-effective-dartSkill
- deploying-with-fastlaneSkill
- integrating-firebaseSkill
- implementing-fl-chartSkill
- integrating-google-adsSkill
- animating-flutter-widgetsSkill
- routing-with-autorouteSkill
- configuring-deep-linksSkill
- persisting-data-with-driftSkill
- building-flutter-production-appsSkill
- generating-flutter-uiSkill
- routing-with-gorouterSkill
- managing-hive-storageSkill
- using-flutter-hooksSkill
- running-flutter-isolatesSkill
- managing-flutter-provider-stateSkill
- implementing-responsive-layoutsSkill
- implementing-riverpodSkill
- managing-secure-storageSkill
- flutter-setupSkill
- managing-shared-preferencesSkill
- implementing-flutter-social-authSkill
- flutter-testingSkill
- using-fpdartSkill
- generating-freezed-modelsSkill
- configuring-github-actionsSkill
- building-jaspr-web-appsSkill
- testing-with-marionetteSkill
- implementing-openapi-in-dartSkill
- integrating-revenuecatSkill
- integrating-sentrySkill
- developing-serverpod-backendSkill
- building-shadcn-flutter-uiSkill
- integrating-supabaseSkill
- converting-ts-to-dartSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 120
Use Freezed for sealed union states with exhaustive when/map pattern matching.
- Very specific: line 142
Immutable States: All states MUST be immutable.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
BLoC State Management (v9.x)
Goal
Strict separation of UI from business logic via event-driven unidirectional data flow. Choose Cubit (lightweight, no events) for simple state, or full Bloc (events + transformers) for complex flows.
Process
1. Install Dependencies
dependencies:
flutter_bloc: ^9.1.1
equatable: ^2.0.5
bloc_concurrency: ^0.3.0 # For event transformers
dev_dependencies:
bloc_test: ^10.0.02. Choose Pattern
| Pattern | When to Use |
| Cubit | Simple state (toggle, counter, form validation) |
| Bloc | Event tracking, transformers, complex flows (login, search debounce) |
3A. Implement Cubit
class CounterState extends Equatable {
final int value;
const CounterState({required this.value});
@override
List<Object?> get props => [value];
}
class CounterCubit extends Cubit<CounterState> {
CounterCubit() : super(const CounterState(value: 0));
void increment() => emit(CounterState(value: state.value + 1));
void decrement() => emit(CounterState(value: state.value - 1));
}3B. Implement Bloc
abstract class CounterEvent extends Equatable {
const CounterEvent();
@override
List<Object?> get props => [];
}
class CounterIncremented extends CounterEvent {}
class CounterDecremented extends CounterEvent {}
class CounterBloc extends Bloc<CounterEvent, CounterState> {
CounterBloc() : super(const CounterState(value: 0)) {
on<CounterIncremented>((e, emit) => emit(CounterState(value: state.value + 1)));
on<CounterDecremented>((e, emit) => emit(CounterState(value: state.value - 1)));
}
}4. Provide to Widget Tree
MultiBlocProvider(
providers: [
BlocProvider(create: (context) => CounterCubit()),
BlocProvider(create: (context) => AuthBloc()),
],
child: MyApp(),
)5. Consume State
| Widget | Use For |
BlocBuilder | Rebuild UI on state changes |
BlocListener | Side effects only (navigation, snackbars) |
BlocConsumer | Rebuild + side effects together |
// Trigger events
context.read<CounterBloc>().add(CounterIncremented());
context.read<CounterCubit>().increment(); // Cubit: direct method callSee BLoC Best Practices for full widget examples with buildWhen/listenWhen.
6. Event Transformers
| Transformer | Behavior | Use Case |
concurrent (default) | All events in parallel | Independent API calls |
sequential | One at a time, queued | Multi-step transactions |
restartable | Cancel previous, start fresh | Search bar, autocomplete |
droppable | Ignore while processing | Submit button deduplication |
import 'package:bloc_concurrency/bloc_concurrency.dart';
on<SearchQueryChanged>(_onQueryChanged, transformer: restartable());
on<FormSubmitted>(_onFormSubmitted, transformer: droppable());See BLoC Best Practices for custom debounce transformer.
7. Freezed Integration
Use Freezed for sealed union states with exhaustive when/map pattern matching.
See BLoC Best Practices for full Freezed setup and examples.
8. Common Errors
See Error Handling & Common Pitfalls for solutions to:
- Emitting after Bloc close (
emit.isDoneguard) - Mutable state causing silent UI bugs (spread into new instances)
- Scattered
try/catch— useonError+addErrorinstead
Reference Documentation
- BLoC Best Practices (v9.x) — Transformers, Freezed, testing with blocTest, hydrated/replay packages
- Error Handling & Common Pitfalls — Three common errors with before/after fixes
- State Management Overview — Cubit vs Provider/MVVM comparison
Constraints
- Immutable States: All states MUST be immutable. Use
EquatableorFreezed. - No UI in Bloc: Blocs/Cubits MUST NOT import Flutter widgets or
BuildContext. - Single Responsibility: Each Bloc handles one feature domain.
- Event Naming: Past tense nouns (
UserLoggedIn, notUserLogin). - State Naming: Nouns or adjectives (
AuthSuccess, notAuthSucceeded). - Always Close: Blocs/Cubits MUST be closed on dispose to prevent memory leaks.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

