Flutter State Managment

What is Flutter BLoc

Flutter State Managment

Flutter bloc is one of the state management for Flutter applications. You can use it to handle all the possible states of your application in an easy way.

Flutter bloc is simple to use because you and your team are going to understand the concept quickly, no matter what is your level, this library has very good documentation with tons of examples and also, is one of the most used in the flutter community, so if you have any question or problem you probably are going to find the solution with a simple search on the internet.

Is powerful because is going to help you to create all kinds of applications, you can create applications for learning purposes for example, and also you can create complex applications in a production environment and flutter bloc is valid in both cases.

Another important aspect of this library is that you could test your bloc logic in an easy way.

For more information, you can visit the official website: https://bloclibrary.dev/#/gettingstarted

 

Bloc Widgets

These are widgets that the library provides you to manage all the possible cases, for example, add an event, listen to a state, emit a state, rebuild the view depending on the state, and so on.

BlocProvider/MultiBlocProvider

BlocProvider is in charge of providing a bloc to its children. Is the way of “initializing” the bloc before using it.

If you need to provide more than one bloc you can use the MultiBlocProvider to get different providers.

RepositoryProvider/MultiRepositoryProvider

RepositoryProvider is used to provide a repository to its children. Normally you are going to use it when you need to create an instance of your repository class and then with the BlocProvider, you are going to access that repository with help of context.read();

This is an example.

If you need multiple repositories providers you can use a MultiRepositoryProvider.

BlocListener

With this widget, you are going to be able to ‘listen’ to different states that would be emitted from your bloc and then react to them, for example, to show a snackbar, dialog, or navigate to another page... This widget doesn’t rebuild the view, it’s only listening.

BlocBuilder

With this, you are going to be able to rebuild your widgets depending on their state.

BlocConsumer

This widget is super useful when you need to control the states of your bloc to rebuild the widget and also for navigate or show a dialog, etc. This widget has the listener and the builder function so you can use it together.

BlocSelector

This widget allows developers to filter updates by selecting a new value based on the current bloc state.

These explanations are at a high level, there are more ways to use them. For more info, you can check the docs.