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に貼り付けるだけ。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だけのプロジェクトでは、Flutterのウィジェットのパッケージを使わず、blocパッケージを使うことを勧めています。
詳細
作者による元の説明
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
14kFlutterアプリでRiverpodを使って状態を管理する方法を、プロバイダー、非同期データ、テストを含めて、各場合に妥当な初期値とともに、エージェントに教えます。
document-extraction
14kエージェントが、LandingAIのADEサービスを使って、PDF、画像、表計算を読み、きれいなテキストに変え、請求書の合計などの項目を取り出せるよう支援します。
document-workflows
14kエージェントが、多くの文書を一度に処理し、検索ツールに備え、結果をデータベースに読み込む、繰り返し可能なパイプラインを作れるよう支援します。
似ているスキル
android-clean-architecture
280kエージェントがAndroidプロジェクトを効率よく整理できるようにします。コードを構造化して、保守や新機能の開発をしやすくします。
compose-multiplatform-patterns
280kエージェントがKMPプロジェクトでCompose MultiplatformとJetpack Composeのパターンを使えるようにします。状態管理、ナビゲーション、テーマ設定、パフォーマンスを扱います。
dart-flutter-patterns
280kエージェントがDartとFlutterのアプリ向けのコードを書けるようにします。最新のパターンを使い、堅牢で管理しやすいアプリを作ります。