Udemy
BLoC Tutorial -The full documentation on BLoC package is available in bloclibrary.dev. This video tutorial is also available on youtube. If you want, then instead of buying the Udemy tutorial, you can follow the youtube tutorial.
As developers we want to:
- know what state our application is in at any point in time.
- easily test every case to make sure our app is responding appropriately.
- record every single user interaction in our application so that we can make data-driven decisions.
- work as efficiently as possible and reuse components both within our application and across other applications.
- have many developers seamlessly working within a single code base following the same patterns and conventions.
- develop fast and reactive apps.
Bloc was designed to meet all of these needs and many more.
There are many state management solutions and deciding which one to use can be a daunting task. There is no one perfect state management solution!
Bloc was designed with three core values in mind:
- Simple: Easy to understand & can be used by developers with varying skill levels.
- Powerful: Help make amazing, complex applications by composing them of smaller components.
- Testable: Easily test every aspect of an application so that we can iterate with confidence.
Overall, Bloc attempts to make state changes predictable by regulating when a state change can occur and enforcing a single way to change state throughout an entire application.
BLoC Core Concept
- Stream:
It is the foundation of BLoC. The stream is a river , which transport some data, on a boat, from the sender to the receiver. The transported data is asynchronous.
- Cubit:
A cubit is the minimal version of a BLoC. BLoC actually extends cubit.
- Bloc:
BLoC is the big brain of the project. Where as cubit is used to optimize the functionality of the project.
**Note: Start with a cubit. If you see the necessity, then modify your cubit into BLoC.
Flutter BLoC Concept
Some vital Flutter concepts to get going (widget, widget tree).
- BlocProvider + CounterApp implementation.
- BlocBuilder + CounterApp implementation.
- BlocListener + CounterApp implementation.
- BlocConsumer + CounterApp implementation.
- RepositoryProvider
- MultiBlocListener, MultiBlocProvider, MultiRepositoryProvider
`RepositoryProvider is a class. Which has the main function which makes flutter communicate with outer data layer i.e. internet, APIs, databases, etc`
BlocBuilder & BlocListener can be combined together to from BlocConsumer
BLoC Architecture
Like every application, BLoC also follows a specific file pattern. The "Business logic" layer is separated from the "Presentation" and from the "Data" layers.
Complete BLoC architecture
Weather app architecture