agentleFS
VeryGoodOpenSourceVeryGoodOpenSource/vgv-ai-flutter-plugin/skills/bloc/SKILL.md

bloc

Skill168 starsChanged 3 months agoMITView on GitHub

Best practices for Bloc state management in Flutter/Dart, covering Cubit versus Bloc, event and state naming, sealed classes with Equatable, the Page/View split with BlocProvider, BlocBuilder, BlocListener, and BlocSelector. Use when writing, modifying, or reviewing code that uses package:bloc, package:flutter_bloc, or package:bloc_test. Also use for any test of a class that extends Bloc or Cubit, including "write the unit tests for this bloc", "test my cubit", "add tests for LoginBloc", mocking a repository a bloc depends on, and asserting the states an event emits. Bloc and Cubit tests must use blocTest() from package:bloc_test with package:mocktail, so prefer this over the testing skill whenever the class under test is a Bloc or Cubit. Also use when a request pushes against these rules, such as injecting one bloc into another to read its state, calling a repository from a widget or build method, or keeping a Cubit for a feature that needs an event transformer such as debounce.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 1 is distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use when each state carries different data.

    line 121
  2. Very specific:

    Use when all states share the same data shape: one class holding a status enum plus the data fields, with a copyWith for transitions.

    line 153
  3. Very specific:

    Use BlocProvider to supply a Bloc or Cubit to a subtree, then BlocBuilder, BlocListener, BlocConsumer, or BlocSelector to consume it.

    line 177
  4. Very specific:

    context.watch or BlocBuilder in build: Never call context.watch outside a build method.

    line 183

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Bloc

State management library for Dart and Flutter using the BLoC (Business Logic Component) pattern to separate business logic from the presentation layer.


Core Standards

Apply these standards to ALL Bloc/Cubit work:

  • Use blocTest() from package:bloc_test for all Bloc and Cubit tests — never raw test() with manual stream assertions
  • Use package:mocktail for mocking — never package:mockito
  • build: constructs the bloc under test — return a new instance from the callback, never one built in setUp and shared, so every blocTest starts from a clean bloc and its lifecycle stays with blocTest
  • No bloc-to-bloc direct dependencies — blocs communicate through the UI or shared repositories
  • Page/View separation — Page provides the Bloc/Cubit via BlocProvider, View consumes via BlocBuilder/BlocListener
  • Sealed classes for events and multi-state types — enables exhaustive pattern matching with Dart 3 switch
  • Equatable for all states and events — extend Equatable and override props for value equality
  • Business logic in Bloc/Cubit only — never in widgets, pages, or views
  • Single responsibility — one Bloc/Cubit per feature concern
  • Emit only after async checks — use emit only inside the handler callback
  • Dart 3.13 primary constructors — on the Dart 3.13+ baseline (very_good_core 1.6 / very_good_analysis 11), declare event, state, and widget fields as primary-constructor declaring parameters (class const ProfileRequested(final String userId) extends ProfileEvent) rather than this.field; keep the classic form only below 3.13

Cubit vs Bloc

AspectCubitBloc
APIFunctions → emit(state)Events → on<Event> → emit(state)
ComplexityLowHigher
TraceabilityLess (no event log)Full (events + transitions)
When to useSimple state, UI-driven logicComplex flows, event-driven, transforms
TestingCall methods, assert statesAdd events, assert states

Cubit Example

class CounterCubit extends Cubit<int> {
  CounterCubit() : super(0);

  void increment() => emit(state + 1);
  void decrement() => emit(state - 1);
}

Bloc Example

sealed class CounterEvent extends Equatable {
  const CounterEvent();

  @override
  List<Object> get props => [];
}

final class CounterIncrementPressed extends CounterEvent {}
final class CounterDecrementPressed extends CounterEvent {}

class CounterBloc extends Bloc<CounterEvent, int> {
  CounterBloc() : super(0) {
    on<CounterIncrementPressed>((event, emit) => emit(state + 1));
    on<CounterDecrementPressed>((event, emit) => emit(state - 1));
  }
}

Naming Conventions

Events

Pattern: BlocSubject + Noun + VerbPastTense

Event class nameMeaning
TodoListSubscriptionRequestedSubscribing to todo list stream
TodoListTodoDeletedDeleting a specific todo
TodoListUndoDeletionRequestedUndoing the last deletion
LoginFormSubmittedSubmitting the login form
ProfilePageRefreshedRefreshing the profile page
sealed class TodoListEvent extends Equatable {
  const TodoListEvent();

  @override
  List<Object> get props => [];
}

final class TodoListSubscriptionRequested extends TodoListEvent {}

final class const TodoListTodoDeleted({required final Todo todo})
    extends TodoListEvent {
  @override
  List<Object> get props => [todo];
}

States

Subclass Approach (multiple state types)

Use when each state carries different data.

State class nameMeaning
LoginInitialNo action taken yet
LoginInProgressLogin request in flight
LoginSuccessLogin succeeded
LoginFailureLogin failed
sealed class LoginState extends Equatable {
  const LoginState();

  @override
  List<Object> get props => [];
}

final class LoginInitial extends LoginState {}
final class LoginInProgress extends LoginState {}
final class const LoginSuccess({required final User user}) extends LoginState {
  @override
  List<Object> get props => [user];
}
final class const LoginFailure({required final String error})
    extends LoginState {
  @override
  List<Object> get props => [error];
}

Single Class Approach (one state, multiple fields)

Use when all states share the same data shape: one class holding a status enum plus the data fields, with a copyWith for transitions. See references/patterns.md for the full shape.


Architecture

LayerContainsDepends on
PresentationPages, Views, WidgetsBusiness Logic
Business LogicBlocs, CubitsData
DataRepositories, Data ProvidersExternal sources

Data Layer

Repositories abstract data sources and provide a clean API for Blocs/Cubits. Mirror the feature folder structure under test/ for all test files.

See references/architecture.md for the repository example, feature folder structure, and test directory layout.


Flutter Widgets

Use BlocProvider to supply a Bloc or Cubit to a subtree, then BlocBuilder, BlocListener, BlocConsumer, or BlocSelector to consume it. Reach for BlocSelector when a rebuild should depend on one field rather than the whole state, and BlocListener for side effects such as navigation or a snackbar.

The rule that matters: context.read in callbacks (onPressed, onTap), context.watch or BlocBuilder in build. Never call context.watch outside a build method.

Additional Resources

  • references/architecture.md — repository example, feature folder structure, test directory layout
  • references/widgets.md — widget and context extension tables, Page/View pattern, BlocListener example
  • references/testing.md — blocTest() parameters, Cubit/Bloc test examples, mocking dependencies, widget integration tests
  • references/patterns.md — single-class state, adding features with Bloc/Cubit, async operations, event transformers

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.