Flutter is a Google powered app development kit that allows app developers to develop cross-platform applications such as Android, iOS, Web, Windows, and Mac. Day by day, Flutter is gaining popularity, and so it’s developers. To make things easy for devs, Google has introduced the Bloc pattern back in 2018 during its Google I/O event. Since then, it’s been almost a year after introducing bloc, and finally, Bloc Library is updated now! So, before we dig in more for the latest updates, firstly, we need to know more about bloc and how it works with Flutter considering newbies.
What is Bloc?
Bloc stands for Business Logic Component(Bloc), which is a core library developed to reduce the workload on Flutter app developers using the idea of code testability and reusability. To understand and get a better idea of the bloc, you can think of it as a brain. This bloc library simplifies the reactive app development process by allowing Flutter app developers to convert the events into states. And to get bloc output in a state, events, or you can say user input that comes from one side; bloc mirrors the same code in response to generate output. Using the same principle, Flutter UI widgets listen to states and display responses on the screen as a reaction to user action. And, this is how bloc works. However, when you write your Flutter app code from scratch, you need to write much boilerplate code too.
But using Bloc have their advantages, such as…
- To know the current app state
- Simplifies app case testing process
- Record’s user interaction to make data-driven conclusions
- Reuse components within the app as well as in other apps
- Use a single code base with the same pattern
- Faster the app development process
Apart from bloc’s core library, there are two other libraries named flutter_bloc and angular_bloc that work simultaneously with the bloc to the faster app development process. The tremendous growth in the Flutter community has played a significant role in the result of improvising the existing bloc library. However, this library assists you in dealing with the complexities of existing bloc, streams, and sinks. To have the best code practice, you can use their extension for both VS Code and IntelliJ that provide code snippets and instant bloc class creation facility. Now you know about blocs, and it’s working pattern, so let’s move further and see what’s new in the latest Library 1.0.0 update.
What’s new in the latest Bloc Library 1.0.0
1. Bloc as a Stream
In the latest update to bloc library v1.0.0, all blocs are now converted into streams, which means you no longer need to use Stream< state> get state. Instead, the Flutter app developer can quickly know existing state stream directly from the bloc itself. It also helps us to remove the need for using currentState property and reuse the bloc state property to fetch the value of the state. The main reason is to reduce confusion and differentiate both bloc.state and bloc.currentState. Working with the bloc’s state stream should now feel very intuitive, especially to those coming from StreamControllers and Subjects.
2. Bloc as a Sink
With the update to the bloc library, all blocs are converted into Sinks. This makes it even easier for Flutter Developer to continue using the traditional add sink API instead of dispatch to notify the Bloc for a new event. Also, the dispose objected in support of close. From now while using the dartanalyzer, it shows warnings if you forget to call blocs using close. Yet, it is performed naturally by the BlocProvider.
3. Testing Improvements
This time there is one more new package, which is bloc_test that is included in the latest library update. The main aim of the bloc_test package is to simplify stub a bloc’s state stream through whenListen. However, this is still in beta. Still, it is expected to be helpful for developers to test helpers that are specially designed for the bloc library. After this, it is assumed to get more such extra testing helpers that will come preloaded in the upcoming updates.
4. Official Documentation
To make it easier for developers to find bloc library and tutorials under one hood, old pub.dev stuff is now moved into new domain bloclibrary.dev. This time it comes with official documentation so that newbies can learn it quickly. And we recommended you to go through it cause it helps you to plan bloc usage in your next app development process.
So, these were four significant changes that happened in the update to the latest Bloc library. But if you want to see in action before you use it in your next app development project, you can consider the Counter app. Which is pure Dart app made up using CounterBloc? Let us know what you think about this newly introduced Bloc Library v1.0.0 by commenting below.