跳到正文
Skillpedia
移动应用14k · #11269

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/andrewyng/context-hub/blob/main/content/flutter/skills/state-management/bloc-cubit/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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 Cubit for simple, direct state updates.
  • Use Bloc for event-driven flows, transitions, and replayable business logic.
  • Use bloc in Dart-only projects.
  • Use flutter_bloc in Flutter apps when you need widgets like BlocProvider, BlocBuilder, or BlocListener.
  • If you install flutter_bloc, you do not need to add bloc separately in a Flutter app because flutter_bloc depends 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_bloc unless Flutter widgets are needed

For Flutter UI code:

  • add flutter_bloc
  • let it bring bloc transitively
  • use BlocProvider at the feature boundary
  • use BlocBuilder for rebuilds and BlocListener for 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 BlocProvider or MultiBlocProvider to let Flutter manage disposal.
  • If you create a bloc/cubit with new or 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 Cubit by calling methods and asserting emitted states.
  • Test Bloc by 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 Bloc for trivial local state.
  • Do not add both bloc and flutter_bloc in a Flutter app when only flutter_bloc is 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 bloc or flutter_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 package
  • flutter_bloc = Flutter UI integration package
  • Cubit = method-based updates
  • Bloc = 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能