Langsung ke konten
Skillpedia
Aplikasi mobile14k · #11269

bloc-cubit

Fungsinya

Skill ini menjelaskan cara mengatur data dan perubahan layar dalam aplikasi Flutter dengan alat Bloc dan Cubit. Skill ini ditujukan untuk pengembang aplikasi Flutter yang ingin penanganan status yang jelas dan dapat diuji.

Kapan dipakai

  • ✓Saat Anda membangun aplikasi Flutter dan perlu mengelola data layar yang berubah.
  • ✓Saat Anda memilih antara Bloc dan Cubit untuk sebuah fitur.
  • ✓Saat Anda ingin menyiapkan pengujian untuk logika status Flutter.

Link skill

https://github.com/andrewyng/context-hub/blob/main/content/flutter/skills/state-management/bloc-cubit/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/andrewyng/context-hub/blob/main/content/flutter/skills/state-management/bloc-cubit/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit →

Contoh permintaan ke AI

“Saya ingin menyiapkan manajemen status Bloc untuk layar checkout di aplikasi Flutter saya.”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Skill ini memberi aturan sederhana: gunakan Cubit untuk pembaruan langsung dan Bloc untuk langkah berbasis peristiwa. Kemudian skill ini menunjukkan pengaturan dan struktur yang perlu diikuti dalam kode.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari 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()
Pertanyaan umum

Apa perbedaan Bloc dan Cubit?

Cubit menangani pembaruan langsung yang sederhana, sedangkan Bloc memodelkan tindakan pengguna sebagai peristiwa yang eksplisit.

Apakah saya perlu kedua paket di aplikasi Flutter?

Biasanya tidak. Paket flutter_bloc sudah membawa paket bloc inti dengan sendirinya.

Apakah ini untuk proyek Dart biasa?

Untuk proyek Dart saja, skill ini menyarankan paket bloc tanpa paket widget Flutter.

Detail

Deskripsi asli dari pembuat

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

Popularitas

Bintang GitHub
14k
Peringkat
#11269
Lisensi
MIT
Diperbarui
31/5/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa