본문으로 건너뛰기
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에 붙여넣으세요. AI가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

Use this skill to help me: https://github.com/andrewyng/context-hub/blob/main/content/flutter/skills/state-management/bloc-cubit/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
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 전용 프로젝트에는 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.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬