<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Masrafi Anam</title>
    <description>The latest articles on DEV Community by Masrafi Anam (@masrafi_anam_9efdc6e67df1).</description>
    <link>https://dev.to/masrafi_anam_9efdc6e67df1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3454661%2F9748f55c-1b3a-436d-bb16-29e2fc510a67.jpg</url>
      <title>DEV Community: Masrafi Anam</title>
      <link>https://dev.to/masrafi_anam_9efdc6e67df1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/masrafi_anam_9efdc6e67df1"/>
    <language>en</language>
    <item>
      <title>Flutter Bloc Pattern Explained: States &amp; Events for Beginners</title>
      <dc:creator>Masrafi Anam</dc:creator>
      <pubDate>Sat, 23 Aug 2025 15:01:27 +0000</pubDate>
      <link>https://dev.to/masrafi_anam_9efdc6e67df1/flutter-bloc-pattern-explained-states-events-for-beginners-1dal</link>
      <guid>https://dev.to/masrafi_anam_9efdc6e67df1/flutter-bloc-pattern-explained-states-events-for-beginners-1dal</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction — Why Should You Care?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fle95a8cnqiknlt6fj0rn.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fle95a8cnqiknlt6fj0rn.webp" alt=" " width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When flutter apps grow, using setState() everywhere can turn your code into a tangled mess.&lt;br&gt;
Bloc (Business Logic Component) fixes that by separating what happens (events) from what you show (states), keeping your code clean and testable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How the Bloc works?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You do something — like tap a button or scroll the page.&lt;/li&gt;
&lt;li&gt;This action creates an Event — a message that says “something happened.”&lt;/li&gt;
&lt;li&gt;The Bloc gets the event and does the work — maybe call an API, check data, or run some calculations.&lt;/li&gt;
&lt;li&gt;When the work is done, the Bloc sends out a new State — a message about what the app should look like now.&lt;/li&gt;
&lt;li&gt;The UI is always listening, so when the state changes, the screen updates automatically.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Code Example with Bloc&lt;/strong&gt;&lt;br&gt;
Now we show here full login with bloc. Let’s start,&lt;/p&gt;

&lt;p&gt;1️⃣ Events:&lt;br&gt;
Here, we created two events — one for login and one for logout. Remember, events represent actions taken by the user.&lt;br&gt;
// auth_event.dart&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;abstract class AuthEvent {}
class LoginRequested extends AuthEvent {
 final String email;
 final String password;
LoginRequested(this.email, this.password);
}
class LogoutRequested extends AuthEvent {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2️⃣ States&lt;br&gt;
Next, we create the states. In this example, we have three: Loading, Loaded, and Error. You can also add your own, like LoadedDone, if your app requires it.&lt;br&gt;
// auth_state.dart&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;abstract class AuthState {}
class AuthInitial extends AuthState {}
class AuthLoading extends AuthState {}
class Authenticated extends AuthState {
 final String userId;
 Authenticated(this.userId);
}
class AuthError extends AuthState {
 final String message;
 AuthError(this.message);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3️⃣ The Bloc&lt;br&gt;
The Bloc class takes in a user action (event) and outputs the appropriate data (state).&lt;br&gt;
// auth_bloc.dart&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter_bloc/flutter_bloc.dart';
import 'auth_event.dart';
import 'auth_state.dart';
import '../repositories/auth_repository.dart';
class AuthBloc extends Bloc&amp;lt;AuthEvent, AuthState&amp;gt; {
 final AuthRepository authRepository;
AuthBloc(this.authRepository) : super(AuthInitial()) {
 on&amp;lt;LoginRequested&amp;gt;(_onLoginRequested);
 on&amp;lt;LogoutRequested&amp;gt;(_onLogoutRequested);
 }
Future&amp;lt;void&amp;gt; _onLoginRequested(
 LoginRequested event,
 Emitter&amp;lt;AuthState&amp;gt; emit,
 ) async {
 emit(AuthLoading());
 try {
 final userId = await authRepository.login(event.email, event.password);
 emit(Authenticated(userId));
 } catch (e) {
 emit(AuthError(e.toString()));
 }
 }
void _onLogoutRequested(
 LogoutRequested event,
 Emitter&amp;lt;AuthState&amp;gt; emit,
 ) {
 authRepository.logout();
 emit(AuthInitial());
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4️⃣ Using It in UI&lt;br&gt;
This is the UI section. Here, we take the state returned from the Bloc and show it in a ListView.builder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BlocProvider(
 create: (context) =&amp;gt; AuthBloc(context.read&amp;lt;AuthRepository&amp;gt;()),
 child: LoginScreen(),
);
// Inside LoginScreen:
BlocBuilder&amp;lt;AuthBloc, AuthState&amp;gt;(
 builder: (context, state) {
 if (state is AuthLoading) return CircularProgressIndicator();
 if (state is Authenticated) return Text("Welcome, ${state.userId}");
 if (state is AuthError) return Text("Error: ${state.message}");
 return LoginForm();
 },
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why Use Bloc?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Separation of Concerns:&lt;/strong&gt; Keeps business logic out of the UI, making the app easier to maintain.&lt;br&gt;
&lt;strong&gt;Testability:&lt;/strong&gt; Each layer (Bloc, Event, State) can be tested independently.&lt;br&gt;
&lt;strong&gt;Scalability:&lt;/strong&gt; Well-suited for large apps where managing state becomes complex.&lt;br&gt;
&lt;strong&gt;Consistency:&lt;/strong&gt; Provides a predictable state flow, making debugging easier.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What challenges have you faced with Bloc? Share your experiences or questions in the comments!&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
