rectangles-mixedBloc

Why Use Bloc?

Blocarrow-up-right is built on top of Flutter's Providerarrow-up-right and has been marked as a Favorite package by Flutter. It simplifies the code required when using Provider and is currently one of the best state management solutions for Flutter.

Bloc may seem complex for beginners, but compared to simpler state management solutions like GetXarrow-up-right, Bloc offers better screen rendering optimization and more convenient state management. Both Bloc and Riverpodarrow-up-right are based on Provider, so you can choose a state management tool based on your preference. Bloc also provides a simpler state management approach called Cubitarrow-up-right, making it beneficial for both small-scale and large-scale projects.

For large-scale projects, Bloc is commonly used (Bloc conceptsarrow-up-right), but it requires writing a significant amount of code. To address this, Mason can be used as a solution. I will explain how I automate my Bloc code templates in the next section.

In addition to state management, Bloc offers other useful features that I frequently use, such as:

  • Caching and persisting Bloc states, which is useful for storing application settings.

  • Redo and Undo functionalities for state transitions.

For more details, refer to Bloc Packagesarrow-up-right.

Code Faster with Mason

You can check out my Bloc template at dr_blocarrow-up-right and create one for yourself. If you are not familiar with Mason, refer to this guide: Boost Your Flutter Development Efficiency with Masonarrow-up-right.

Code Faster with Mason

My Bloc is designed as part of the MVVM architecture, where the UI communicates directly with the Bloc. The Bloc is responsible for retrieving data from repositories. You can explore the official Bloc architecture herearrow-up-right. If you want to implement it within a clean architecture, check out this article: Maintain and Extend Code Easier with Clean Architecturearrow-up-right.

Most of us get used to a specific coding style, but when switching to different projects, rewriting everything from scratch slows down development. For simple modules, Cubit is a great choice, but for larger and more complex modules, we need a more structured and explicit state management approach.

My Bloc Structure

I organize my Bloc into a single folder with two main parts:

  1. Bloc for handling state logic.

  2. Event and state combined into a single file (since they are not too long), reducing unnecessary files.

This structure allows us to define state enums or default app settings at initialization.

This Section Will Contain Bloc Events

This section will contain the Bloc, where you can customize your functions.

In the Bloc file, I have pre-written code for Cubit. You can uncomment it to use, or remove it if you find it unnecessary.

To make the Bloc work, we need to add it to the BlocProvider. Below is an example of declaring a Bloc before using it:

Make sure to explore Bloc extensions for IDEs like Android Studio to speed up your development process. You can also create live templates to reuse frequently used code snippets efficiently.

Debugging and Logging Bloc Events

This feature is already covered in the Bloc documentation. Typically, it's only necessary for critical Blocs where you need to monitor event flow or debug errors.

Another way to always listen to these changes without writing them into each individual Bloc is available.

Call it after the application has fully initialized, as shown below:

Persisting Bloc State to Device Storage

To save the Bloc state to device storage for the next app launch, refer to: 🔗 Replay Bloc Documentationarrow-up-right

Step 1: Replace Event Definition

Change:

To:

Step 2: Replace Bloc Definition

Change:

To:

Then, import these two libraries:

Step 3: Add JSON Serialization Functions

Inside {{name.pascalCase()}}Bloc, implement the following:

Step 4: Initialize HydratedBloc After App Launch

Make sure to initialize HydratedBloc after the app has fully loaded. If you're not using MyFlutter, replace it with the appropriate storage directory for your project. More details: Hydrated Bloc Usagearrow-up-right

Calling Bloc from Anywhere in the Project

Here is an example of how I can access context from anywhere:

Check out my article on accessing context anywhere here to learn how to set it up.

message-middleContext Anywherechevron-right

This is how I use Bloc in my projects. You can adapt it to create your own code template that fits your projects, helping reduce development time while still leveraging the full power of Bloc. Buy Me a Coffeearrow-up-right | Support Me on Ko-fiarrow-up-right

Last updated