bloc-cubit
它能做什么
这项技能说明如何使用 Bloc 和 Cubit 工具,在 Flutter 应用中组织数据和界面变化。适合希望状态处理清晰、可测试的 Flutter 应用开发者。
适用场景
- ✓当你在构建 Flutter 应用,需要管理变化的界面数据的时候。
- ✓当你在为某项功能在 Bloc 和 Cubit 之间做选择的时候。
- ✓当你想为 Flutter 状态逻辑设置测试的时候。
技能链接
https://github.com/andrewyng/context-hub/blob/main/content/flutter/skills/state-management/bloc-cubit/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我想为 Flutter 应用中的结账页面设置 Bloc 状态管理。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能给出一个简单的规则:直接更新用 Cubit,事件驱动的步骤用 Bloc。然后它展示在代码中应遵循的设置和结构。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
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.
Testing Rules
- Test
Cubitby calling methods and asserting emitted states. - Test
Blocby adding events and asserting the transition sequence. - Mock repositories at the boundary, not inside the bloc logic.
- Test side effects separately from rendering logic.
Common Pitfalls
- Do not put network calls directly in widgets.
- Do not use
Blocfor trivial local state. - Do not add both
blocandflutter_blocin a Flutter app when onlyflutter_blocis needed. - Do not forget
close()for manually managed instances. - Do not emit duplicate states unless the transition is meaningful.
- Do not let a single bloc grow into an app-wide dumping ground.
What To Prefer In Answers
When writing code or advising on design:
- show the smallest working Bloc or Cubit first
- mention why Bloc or Cubit was chosen
- mention whether the dependency should be
blocorflutter_bloc - include cleanup and testing notes if lifecycle is manual
- keep examples aligned with the current Flutter state management docs and the bloc package docs
Minimal Reference Checklist
bloc= core logic packageflutter_bloc= Flutter UI integration packageCubit= method-based updatesBloc= event-based transitions- manual creation = manual
close() - provider-owned instance = no manual
close()
常见问题
Bloc 和 Cubit 有什么区别?
Cubit 处理简单的直接更新,而 Bloc 将用户操作建模为明确的事件。
Flutter 应用中两个包都需要吗?
通常不需要。flutter_bloc 包会自动引入核心的 bloc 包。
这适用于普通的 Dart 项目吗?
对于纯 Dart 项目,该技能建议使用 bloc 包,而不使用 Flutter 组件包。
详情
作者的原始描述
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.
repo: andrewyng/context-hub
热度
- GitHub 星标
- 14k
- 排名
- #11269
- 许可证
- MIT
- 更新时间
- 2026/5/31
评论
- …
同一仓库的更多技能
get-api-docs
14k帮你的智能体在编写代码之前获取第三方 API 和库的最新文档,而不是依赖旧的记忆。
riverpod
14k教你的智能体如何用 Riverpod 管理 Flutter 应用的状态,涵盖 provider、异步数据和测试,并为每种情况提供合理的默认设置。
document-extraction
14k帮你的智能体读取 PDF、图片和电子表格,将其转换为干净的文本,并使用 LandingAI 的 ADE 服务提取发票总额等字段。
document-workflows
14k帮你的智能体构建可重复使用的流程,一次处理大量文档,为搜索工具做好准备,并将结果导入数据库。