BLoC — The Magic of Single State Class

bloc — the-magic-of-single-state-class

BLoC — The Magic of Single State Class 💙

We have all used BLoC as a State Management tool for our Flutter applications. Normally, we have been using multiple state classes in our BLoC. In this article, let’s explore how you can use just a single state class with copyWith methods to streamline your state management!

Introduction

BLoC is one of the widely used State Management in Flutter Community and production-level apps. However, most of the developers use multiple state classes for BLoC. By multiple state class, we mean having a LoadingState, LoadedState, ErrorState, etc.

But, there are a few disadvantages to this approach. One drawback is the potential increase in complexity and code duplication. With multiple state classes, developers need to manage and coordinate state updates across different components, which can lead to more intricate communication logic and potential bugs. Additionally, having numerous state classes may result in a larger codebase, making it harder to navigate and maintain. It can also introduce challenges when it comes to sharing state between components, as they may require additional synchronization mechanisms. Overall, the use of multiple state classes in BLoC can diminish code simplicity and increase the cognitive load on developers.

Let’s see how you can make the best use of a Single State Class in your BLoC to overcome these drawbacks!

https://medium.com/media/eec832ed536e8718fe95660960a88136/href

Traditional Way/Multiple State Classes BLoC

We will take an example of a simple API call which is made through BLoC. Let’s first see what it looks like with multiple state classes:

home_state.dart

https://medium.com/media/86e329ae3841e3ada6c0325480c2b294/href

home_bloc.dart

https://medium.com/media/f8c7ebe51112b98801ad208e4bd0edeb/href

And here’s how we would be using this in our UI:

https://medium.com/media/ff4cf47ffc44b8f5b908165d4626d28f/href

Pretty simple, right? But what if you want to access photosModel when there is an error too? There are many use cases where you want to access certain variables in a type of state which doesn’t have that variable’s scope.

So, for this, it would be a better option to have a single state class which has the scope of all the variables present 🤩

Let’s now work on our single state class which will do the same thing but with a single state class and an enum for our app status like initial, loading, loaded, and error.

https://medium.com/media/85392e9c7e72af95d467ab501c153466/href

Single State Class BLoC

Let’s now see how we can manipulate BLoC to use a Single State Class with an enum and copyWith method.

home_state.dart

https://medium.com/media/f12ab9d0776a42bc4ff1337cae0a84f3/href

So in the above code snippet, we have a single class called HomeState. There is a static method called initial() which just returns an object of HomeState with initial values. There is another method called copyWith method that updates the value based on the value passed in the method. If for a particular parameter, any value is not passed, it takes the value passed to the HomeState constructor. So, for example, when we use copyWith method and don’t pass any value for status, it will assign HomeStatus.initial because that’s what we passed in the initial method.

Now, let’s see how the bloc file will look:

home_bloc.dart

https://medium.com/media/ac8a9117d01a311b075861691d8f8d79/href

In the above code snippet, you can see that we emitted the state object by updating the value of status and later when API was successful, we emitted the state object by updating the value of status and photosModel.

That’s simple, right? Now, in the UI, we just need to check the value of the status. Let’s see what it looks like:

https://medium.com/media/3b0e4c41f346e5b435f19d73e817094d/href

Anddddd, that’s it 🥳
We can now just use a single state class with an enum for different statuses of the app!

https://medium.com/media/e129c2f68a12ddbe0b223f32f9e55a62/href

Check out the entire example in GitHub Repository 💙

Hope you enjoyed this article!

Doubts? Feel free to drop a message @AbhishekDoshi26

Don’t stop, until you are breathing!💙
– Abhishek Doshi


BLoC — The Magic of Single State Class 💙 was originally published in Google Developer Experts on Medium, where people are continuing the conversation by highlighting and responding to this story.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
what-is-resource-allocation?-resource-allocation-in-project-management

What Is Resource Allocation? Resource Allocation in Project Management

Next Post
iloc-function-in-python

iloc function in python

Related Posts