Skip to main content

Posts

Showing posts from October, 2025

BLoC (Business Logic Component) state management in Flutter

  Let’s go step by step — you’ll understand BLoC (Business Logic Component) state management in Flutter clearly and practically with a real example . 🧠 What is BLoC? BLoC helps separate your UI (presentation) from your business logic . It follows the principle: UI sends events → BLoC processes them → emits new states → UI rebuilds. So instead of writing logic inside your setState() , you handle logic inside the BLoC , and the UI just listens. ⚙️ Core Concepts Concept Role Example Event Represents what happened (user actions) IncrementEvent , FetchUserEvent State Represents the UI condition CounterInitial , CounterUpdated(5) Bloc Handles logic, maps Events → States CounterBloc BlocProvider Makes a Bloc available to widgets BlocProvider(create: (_) => CounterBloc()) BlocBuilder Rebuilds UI when state changes Updates counter display automatically 🧩 Example: Counter App using BLoC Let’s build a simple counter app using flutter_bloc package. 🪜 Step 1: Add Dependency In pu...