bloc-cubit
Use when working with Flutter Bloc/Cubit state management. Covers when to choose Bloc vs Cubit, how to use bloc and flutter_bloc together, lifecycle, testing, and safe defaults.
Оригинален текст · няма потвърден превод на българскиПърво прочетете инструкцията.
Файлът се зарежда от посочения източник само по ваше действие. Можете да го прочетете тук, да го копирате или да го запазите като .md.
GitHub хранилище- Звезди на репото
- 13 979
- Лиценз
- MIT
- Последна проверка на записа
- 20.09.2026 г.
Добавете в своя инструмент.
Командата отваря избора на skills от репозитория. Прочетете файла и условията на автора преди изпълнение.
npx skills add andrewyng/context-hubСтраницата копира текст. Не инсталира и не изпълнява нищо на устройството ви.
Откъс от SKILL.md
Началото на файла на автора, без превод.
Flutter Bloc/Cubit State Management
Use this skill when building Flutter state management with bloc and flutter_bloc.
Core Rule
- Use
Cubitfor simple, direct state updates. - Use
Blocfor event-driven flows, transitions, and replayable business logic. - Use
blocin Dart-only projects. - Use
flutter_blocin Flutter apps when you need widgets likeBlocProvider,BlocBuilder, orBlocListener. - If you install
flutter_bloc, you do not need to addblocseparately in a Flutter app becauseflutter_blocdepends on it.
Decision Guide
Choose Cubit when:
- state changes are simple method calls
- you do not need events
- the feature is local and low complexity
- examples include counters, toggles, filters, form flags, and theme mode
Choose Bloc when:
- user actions should be modeled as explicit events
- the flow has loading, success, and failure transitions
- the logic benefits from clear state machines
- examples include auth, pagination, checkout, sync, and multi-step workflows
Required Project Setup
For Dart-only code:
- add
bloc - do not add
flutter_blocunless Flutter widgets are needed
For Flutter UI code:
- add
flutter_bloc - let it bring
bloctransitively - use
BlocProviderat the feature boundary - use
BlocBuilderfor rebuilds andBlocListenerfor side effects
Implementation Pattern
Prefer this structure:
- repository or service owns I/O
- bloc/cubit owns state and orchestration
- UI only dispatches actions and renders state
Keep state immutable.
Keep events explicit when using Bloc.
Keep one bloc or cubit per feature responsibility.
Lifecycle Rules
- Close manually created blocs and cubits with
close(). - Do not manually close instances owned by
BlocProvider. - Use
BlocProviderorMultiBlocProviderto let Flutter manage disposal. - If you create a bloc/cubit with
newor a constructor outside the widget tree, you own its lifecycle.
UI Binding Rules
Use BlocBuilder when the widget should rebuild from state.
Use BlocListener when the widget should react without rebuilding.
Use BlocConsumer only when both are needed in one place.
Use buildWhen and listenWhen when rebuilds or listeners need narrowing.
Use BlocSelector when only one field should drive rebuilds.