Przejdź do treści
Biblioteka skilli
Programowanie · Budowanie i kod

bloc

/bloc

Podaje dobre praktyki zarządzania stanem Bloc we Flutter i Dart: Cubit kontra Bloc, nazewnictwo zdarzeń i stanów, klasy sealed z Equatable, podział Page i View oraz BlocBuilder, BlocListener i BlocSelector wraz z testami.

Licencja
MIT
Ostatnia zmiana repozytorium
20.10.2026

Ocena i bezpieczeństwo

100/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
6
Skrypty
0
Komentarze HTML
0
Adresy URL w SKILL.md
0
Przestarzałość

Brak sygnałów. Repozytorium zmienione 20.10.2026, bez odwołań do wycofanych modeli i API.

Licencja

MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.

Co robi i kiedy

Podaje dobre praktyki zarządzania stanem Bloc we Flutter i Dart: Cubit kontra Bloc, nazewnictwo zdarzeń i stanów, klasy sealed z Equatable, podział Page i View oraz BlocBuilder, BlocListener i BlocSelector wraz z testami.

Wyzwalacze z opisu

write the unit tests for this bloctest my cubitadd tests for LoginBloc
Oryginalny opis (EN)

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

Jak działa

  1. Core Standards
  2. Cubit vs Bloc
  3. Naming Conventions
  4. Architecture
  5. Flutter Widgets
  6. Additional Resources
Sekcje z nagłówków SKILL.md, w oryginale · 176 linii, 881 słów
Podgląd SKILL.md
---
name: bloc
description: >
  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.
allowed-tools: Read Glob Grep
---

# 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 inside the handler** — call `emit` from the `on<Event>` callback or Cubit method that owns it, never from a stream subscription or callback that outlives the handler
- **Dart 3.13 primary constructors** — on a Dart 3.13+ baseline, 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

| Aspect       | Cubit                         | Bloc                                    |
| ------------ | ----------------------------- | --------------------------------------- |
| API          | Functions → `emit(state)`     | Events → `on<Event>` → `emit(state)`    |
| Complexity   | Low                           | Higher                                  |
| Traceability | Less (no event log)           | Full (events + transitions)             |
| When to use  | Simple state, UI-driven logic | Complex flows, event-driven, transforms |
| Testing      | Call methods, assert states   | Add events, assert states               |

### Cubit Example

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

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

### Bloc Example

```dart
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 name                | Meaning                         |
| ------------------------------- | ------------------------------- |
| `TodoListSubscriptionRequested` | Subscribing to todo list stream |
| `TodoListTodoDeleted`           | Deleting a specific todo        |
| `TodoListUndoDeletionRequested` | Undoing the last deletion       |
| `LoginFormSubmitted`            | Submitting the login form       |
| `ProfilePageRefreshed`          | Refreshing the profile page     |

```dart
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 name  | Meaning                 |
| ----------------- | ----------------------- |
| `LoginInitial`    | No action taken yet     |
| `LoginInProgress` | Login request in flight |
| `LoginSuccess`    | Login succeeded         |
| `LoginFailure`    | Login failed            |

```dart
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](references/patterns.md) for the full shape.

---

## Architecture

| Layer              | Contains                     | Depends on       |
| ------------------ | ---------------------------- | ---------------- |
| **Presentation**   | Pages, Views, Widgets        | Business Logic   |
| **Business Logic** | Blocs, Cubits                | Data             |
| **Data**           | Repositories, Data Providers | External 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](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](references/architecture.md) — repository example, feature folder structure, test directory layout
- [references/widgets.md](references/widgets.md) — widget and context extension tables, Page/View pattern, `BlocListener` example
- [references/testing.md](references/testing.md) — `blocTest()` parameters, Cubit/Bloc test examples, mocking dependencies, widget integration tests
- [references/patterns.md](references/patterns.md) — single-class state, adding features with Bloc/Cubit, async operations, event transformers
Pliki w paczce (6)
  • SKILL.md 8 KB
  • agents/openai.yaml 1 KB
  • references/architecture.md 1 KB
  • references/patterns.md 3 KB
  • references/testing.md 5 KB
  • references/widgets.md 3 KB

Wersje

RepozytoriumWynikLicencja
Ta strona · verygoodopensource/vgv-ai-flutter-plugin100MIT
evanca/flutter-ai-rules100MIT

Migawka bazy z 13.11.2026 · wersja paczki 1894d3e3997b (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 100flutter-bloc
    Zarządza stanem Flutter przez Bloc i Cubit: zdarzenia, stany, transformery i testy z bloc_test.
  • 100bloc-cubit
    Pomaga w zarządzaniu stanem we Flutterze z Bloc i Cubit: wybór między nimi, współpraca bloc z flutter_bloc, cykl życia, testowanie i bezpieczne ustawienia domyślne.
  • 100clean-arch-ui
    Łączy widżety Fluttera ze stanem Cubit lub Bloc, obsługuje ładowanie i błędy, lokalizację i zdarzenia prezentacji w warstwie UI.
  • 100flutter-bloc-state-management
    Wdraża stan, zdarzenia, przejścia i współbieżność asynchroniczną w BLoC i Cubit we Flutterze, w tym zdarzenia z debounce, anulowalne i testy bloc.
  • 100clean-arch-presentation
    Pisze klasy Cubit i Bloc we Flutterze: stany, zdarzenia, wywołania przypadków użycia, efekty jednorazowe i mapowanie błędów.
  • 90flutter-testing
    Pisze i uruchamia testy Fluttera: jednostkowe, dla Cubit i BLoC, repozytoriów, widgetów i integracyjne, zgodnie z konfiguracją projektu.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.