Loading...

Flutter Sealed BLoC Tutorial: Advanced BLoC Concepts


If you want to become a pro developer then you have to throw your noob colleague on the like button and it really works just try it.

If you are thinking this will be complex then you made a mistake because it is really simple just you have to focus on this article as if you are calculating the money. Because once you get the idea then definitely you will choose sealed_flutter_bloc while developing your next project.

Code with simple Bloc

Imagine you are developing your app with the bloc which has different distinct states.

All the above states are subclasses of the MyState base class. It looks so simple.

Now we will check which state has emitted from the bloc so based on that we will return the widget.

Now if you see the above code do you know what is missing? Oh, I forgot to manage the Failure state. So in that case, the bloc will not give any indication for state handling. We must have to remember that and define all the states and return the widget based on conditions and it is not a good thing for developers.

Sealed Flutter Bloc for relief

Let’s see how this helps in our code and why it is important.

Just you have to define the dependency in pubspec.yaml file to get access to the classes.

Here, we have extended class Union4Impl. Because we have 4 states but if you have a number of states then you can change that number with UnionNImpl.

N should be 1 to 9.

Also, if you see the code we have used the Quartet class. it is the same as above because we have 4 states in this example. so, if you have a number of states then you can use them as per your needs. check the below details:

If you see the bloc builder we have implemented SealedBlocBuilder4 but you can replace that number with your number of states.

Still, you are confused don’t worry I have created one example for you. Just you have to give the star and then you will be a pro in flutter sealed bloc.

Hello, I am Sahil Hamirani. A noob developer and noob writer. You can find me on Medium or follow me on GitHub.

Post your comment about which package you want an explanation or example.

 

Leave a comment

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