bloc-cubit
Skill này làm gì
Skill này giải thích cách tổ chức dữ liệu và các thay đổi màn hình trong ứng dụng Flutter bằng công cụ Bloc và Cubit. Skill dành cho lập trình viên xây ứng dụng Flutter muốn quản lý trạng thái rõ ràng và dễ kiểm thử.
Khi nào nên dùng
- ✓Khi bạn xây ứng dụng Flutter và cần quản lý dữ liệu màn hình đang thay đổi.
- ✓Khi bạn đang chọn giữa Bloc và Cubit cho một tính năng.
- ✓Khi bạn muốn thiết lập test cho logic trạng thái trong Flutter.
Liên kết skill
https://github.com/andrewyng/context-hub/blob/main/content/flutter/skills/state-management/bloc-cubit/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Tôi muốn thiết lập quản lý trạng thái Bloc cho màn hình thanh toán trong ứng dụng Flutter của mình.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill đưa ra một quy tắc đơn giản: dùng Cubit cho cập nhật trực tiếp và Bloc cho các bước theo sự kiện. Sau đó skill chỉ cách thiết lập và cấu trúc mã cần theo.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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()
Câu hỏi thường gặp
Bloc và Cubit khác nhau thế nào?
Cubit xử lý các cập nhật trực tiếp đơn giản, còn Bloc mô tả các hành động của người dùng thành các sự kiện rõ ràng.
Tôi có cần cả hai gói trong ứng dụng Flutter không?
Thường là không. Gói flutter_bloc tự động kéo theo gói bloc lõi.
Skill này có dành cho dự án Dart thông thường không?
Với dự án chỉ dùng Dart, skill gợi ý gói bloc mà không có gói widget của Flutter.
Chi tiết
Mô tả gốc của tác giả
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
Độ phổ biến
- Sao GitHub
- 14k
- Vị trí xếp hạng
- #11269
- Giấy phép
- MIT
- Cập nhật
- 31/5/2026
Bình luận
- …
Thêm skill từ kho mã này
get-api-docs
14kGiúp agent của bạn tải tài liệu hiện tại cho các API và thư viện bên thứ ba trước khi viết mã, thay vì dựa vào trí nhớ cũ.
riverpod
14kDạy agent của bạn quản lý trạng thái Flutter bằng Riverpod: provider, dữ liệu bất đồng bộ và kiểm thử, với mặc định hợp lý cho từng trường hợp.
document-extraction
14kGiúp agent của bạn đọc PDF, hình ảnh và bảng tính, chuyển thành văn bản sạch và lấy các trường như tổng hóa đơn bằng dịch vụ ADE của LandingAI.
document-workflows
14kGiúp agent của bạn xây dựng các quy trình lặp lại để xử lý nhiều tài liệu cùng lúc, chuẩn bị cho công cụ tìm kiếm và nạp kết quả vào cơ sở dữ liệu.
Skill tương tự
android-clean-architecture
280kGiúp agent của bạn tổ chức dự án Android hiệu quả, cấu trúc code để việc bảo trì và phát triển tính năng mới dễ dàng hơn.
compose-multiplatform-patterns
280kGiúp agent của bạn dùng các mẫu Compose Multiplatform và Jetpack Compose trong dự án KMP, gồm trạng thái, điều hướng, giao diện và hiệu năng.
dart-flutter-patterns
280kGiúp agent của bạn viết code cho ứng dụng Dart và Flutter. Agent dùng các mẫu hiện đại để tạo ứng dụng vững chắc và dễ quản lý.