60 Days of Flutter : Day 12–14 : Understanding BLoC Pattern in Flutter
As the title suggests, today I’m not going to code anything. I spent the past three days reading different articles and watching videos about BLoC. And I can’t emphasize enough how important having a sound architecture is for a app. So today we’ll focus on understanding the core concepts of BLoC pattern.
Need For an Architecture
During the starting days of my career as a Android Developer back in 2015, like every novice programmer I couldn’t understand what was the need of an architecture after-all. I sure as hell loved programming, the first few apps I built, worked pretty well and so being the novice programmer I was, I didn’t feel the need to learn anything about architectures.
Time is the best teacher.
Yes, and I learnt it the hard way. By the start of 2016 I had lot of work on my hand and continuously maintaining and adding features to the apps I was working on, became a nightmare for me. It was around this time that I realized how a sound architecture and testing could save me a lot of time. It was around this time when my brain shifted to thinking more like a Developer and less like a Programmer.
What is a BLoC ?
So What is a BLoC? A BLoC is an interface between the data sources in your app (Firebase, Database, Rest API) and the UI. It’s used for State Management in Flutter. It takes the user’s inputs as Events, handles the business logic, calls the repository if necessary and returns a State back to the UI.
Let’s go through all the key parts of the above diagram.
- UI : Contains the Widgets and the screen the user interacts with. Every interaction like a button click, a scroll, etc can be mapped to a
Eventand passed on to the BLoC. - BLoC : A BLoC takes the current
Stateand theEvent, maps it to a different State by using the necessary business logic. It is also responsible for interacting with the repository for any data required. - Repository : The repository acts as a interface between a BLoC and all the providers. In the diagram we have multiple providers in form of Firebase and Local DB. The repository is responsible for deciding which of these providers to call.
- Providers : The providers call the actual data sources. All the network call related codes and all the DB query related code are kept in these providers. The result from the providers are passed on to the repository.
- Data Source : These are the actual data sources.
What’s the Need of all this and How is BLoC Pattern Useful?
Using BLoC pattern has quite some benefits, like :
- Helps make code modular.
- Makes it easier to test the business logic.
- Code re-usability.
- The overall layered architecture makes it easy to swap in and out sections. For example switching from a Firebase Provider to a similar REST API Provider can be easily done.
What’s Under the Hood?
Under the hood BLoC uses Dart’s StreamBuilder APIs and RxDart. If you aren’t familiar with streams and reactive programming I’ll highly recommend you checkout this intro.
Implementation in Flutter
We’ll need to implement three things for it:
- Events: They are the input to a Bloc. They are commonly UI events such as button clicks.
Eventsare dispatched and converted toStates. - States: These are the output of a Bloc. Presentation components can listen to the stream of states and redraw portions of themselves based on the given state.
- BLoCs: The BLoCs take the current
StateandEventas inputs and giveStatesas output. Every BLoC class uses themapStateToEventmethod for this mapping.
A Real Usage Example
Let’s say we have a screen with a login form. The possible bare minimum states, events and flow can be:
From the diagram we can see that the LoginEvent and the starting UninitializedState are passed to the BLoC on button press. The username and password are passed as parameters with the LoginEvent. The BLoC calls the repository, and based on the response received from it (Auth Success/Auth Fail) shows the Home Page or the Login Failed Page.
There can be more possible states and events like Forgot password, new registration, etc. as well.
Summary
I started the series by building up the UI first, now we’re done with major sections of the UI. We’ll start building the rest of the app. In the next post we’ll add Firebase to our app and implement it’s Provider. Later on we’ll build our first set of states, events and BLoC classes for the Register Page. Stay tuned !
Also, here are all the videos/blogs I went through for understanding BLoCs :
- State Management | Foundation — flutter_bloc package | Part 1
- Architect your Flutter project using BLOC pattern
- Managing the state of a Widget using bloc | Flutter
- A flutter project showcasing possibilities of bloc pattern
That’s all for today. Cheers !
PS: Do not confuse BLoC pattern with flutter_bloc . BLoC is a pattern whereas flutter_bloc is just a package which helps in implementing the pattern.
How Can You Contribute?
- Open issues with suggestion of better approaches or ideas for the app.
- Connect with me on Twitter or Linkedin or Instagram.
- Star the Github repository.
- Share the series on Twitter.
- Follow me on Github.
Posts In This Series
- 60 Days Of Flutter : Building a Messenger from Scratch
- 60 Days of Flutter : Day 1 : Creating the App
- 60 Days of Flutter : Day 2 : Setting Up A CI With Flutter
- 60 Days of Flutter : Day 3–4 : Building a Chat Screen in Flutter
- 60 Days of Flutter : Day 4–5 : Widget Testing With Flutter
- 60 Days of Flutter : Day 6–7 : Implementing a Slideable Widget Using Bottomsheet in Flutter
- 60 Days of Flutter : Day 8 : Changing The Launcher Icon and Implementing GestureDetector
- 60 Days of Flutter : Day 9–10–11 : Creating Awesome Register Screen in Flutter
- 60 Days of Flutter : Day 12–14 : Understanding BLoC Pattern in Flutter
- 60 Days of Flutter : Day 15–17 : Implementing Registration Screen using ‘flutter_bloc’
- 60 Days of Flutter : Day 18–19 : Unit Testing in Flutter using ‘ mockito’
- 60 Days of Flutter : Day 20–21 : Unit Testing a Bloc in Flutter
- 60 Days of Flutter : Day 22–23 : Building a Modern Contacts Page in Flutter
- 60 Days of Flutter : Day 24–26 : Building a Animated Progress Fab and the Contacts Bloc in Flutter
- 60 Days of Flutter : Day 27–29 : Sending and Retrieving Messages from Firebase using BLOC
- 60 Days of Flutter : Day 30–32 : Firebase Chat UI using Stream and Bloc
- 60 Days of Flutter : Day 33–35 : Paginating data from Firestore using Firebase Queries
- 60 Days of Flutter : Day 36–38 : Seamlessly Upload Files to Firebase Storage
- 60 Days of Flutter : Day 39–41 : One UI Inspired Attachments Showcase Page
- 60 Days of Flutter : Day 42–45 : Creating the Home Page & Quick Peek BottomSheet for Messages
- 60 Days of Flutter : Day 45–47 : Adding Dark Mode to a Flutter App
- 60 Days of Flutter : Day 48–50 : Creating the Settings Page using Bloc
- 60 Days of Flutter : Day 51–54 : Unit Testing Firebase Providers with Mockito
- 60 Days of Flutter : Day 55–56 : Deploying Firestore Security Rules using Firebase CLI
- 60 Days of Flutter : Day 60 : Wrapping It Up
Show Your Support
Press the clap button below if you liked reading this post. The more you clap the more it motivates me to write better!
