Now, you must override the getter for initialState and provide the initial state. You can see that to make a bloc you need to inherit Bloc, and provide types for (1) events and (2) state itself. Then, we will define the bloc itself: class CounterBloc extends Bloc get initialState => 0 As your events become more complex, you might switch to using classes for them, but enum will do for now. The enum keyword defines a enumeration, which is perfect for our case. Since need to add or remove 1 from counter, let us name them increase and decrease: import 'package:bloc/bloc.dart'
#FLUTTER BLOC HOW TO#
Now, we are ready to start using BLoC! Create a new Flutter app (read this tutorial first if you do not know how to do it) and create a new file counter.dart.
#FLUTTER BLOC UPDATE#
Add the following (maybe update the versions if the current year is not 2020) to your pubspec.yaml: bloc: ^4.0.0 Firstly, you need to include it in your dependencies. It is the same one that you see with every new project, but this time you will use BLoC to do it. I will walk you through using BLoC to build the simplest of apps, the counter.
#FLUTTER BLOC CODE#
The nice thing about repositories is that you can have multiple repositories (for local and remote state, or for different SQL dialects) and you do not have to change anything in your business logic code while switching between them! How do I use it? The repository provides access to it, by means of REST, GraphQL, SQL, or other methods. Unlike regular state, long-term state is usually stored in a database, either locally or remotely. The bloc uses the repository to access the long-term state.
![flutter bloc flutter bloc](https://ducafecat.tech/2020/12/08/flutter-bloc/flutter-bloc-01-counter-quick-start/2020-12-08-17-38-28.png)
The repository is something not included in the original Flux architecture, but very useful nonetheless. Widgets receive new state and re-render using a different theme. After the transition is applied, streams notify of this change every UI Widget that cares for it. The bloc will receive the event, and prepare the transition from old state (light theme) to new state (dark theme). This button will send an event when user presses it. In your UI, there is a button to change the theme. You create a bloc that holds the current theme, either light or dark. Suppose that you are building an app that has a dark theme and a light theme, and it is light theme by default. Let me explain this architecture more clearly with an example. After a transition is applied, streams are notified and UI reflects the changes to the state. A transition is a change from one state to another in response to an event. An event is sent to the bloc, and bloc handles it by applying transitions. A stream is something you can subscribe to and be notified every time the state changes. A Bloc holds the state and provides streams of this state.
![flutter bloc flutter bloc](https://img-blog.csdnimg.cn/20200708145118604.png)
Here you can see a simplified flowchart of how BLoC works.
![flutter bloc flutter bloc](https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=https:%2F%2Fblog.kakaocdn.net%2Fdn%2Fb09YhX%2FbtqwbzeFYj5%2FKgu3ZSkoiM2K3IkdZbZcn1%2Fimg.png)
Those coming from React background will immediately recognize it as a Redux sort of thing, and they do have a lot of similarities. BLoC also provides an easy way to subscribe your Widgets to part of the state and implement logic to modify the state. It holds data that describes a particular state of your application (can be literally everything, from theme and locale to value in a text box). What is BLoC?īLoC stands for Business Logic Component, and is, essentially, a global state manager. It is very easy to mess things up when using both these concepts, but I am going to make your life a little easier using reactive programming and BLoC (Business Logic Component) library. Dart, being a multi-paradigmatic language, lets you mix and match OOP and functional programming approaches. How to architect a Flutter application is a question that has no easy answer.