![]() You can do many things with Cubit and don’t need Bloc all the time. Took some deviations from the tutorial and based on the experiance here are some notes to self: You can find the codebase of this application here.A sample application to learn flutter bloc the correct way. State persistence can be a necessity and provide a seamless and user friendly experience to users of our application.Īchieving this can be in order, and simple as possible using the hydratedBloc pattern as demonstrated above. And whenever our app is restarted, it listens to our states and data ,which has been saved to our previous states so it won’t be lost. When our state goes through our bloc class, it gets saved by default because Hydrated BLoC uses Hive under the hood to persist data. To achieve this, we have to override the fromJson and toJson methods in our bloc class, which extends HydratedBloc or uses the HydratedMixin. By this, I mean we have to convert it to JSON format. In order to store our data, we need to serialize it for more complex models. Hence, this is less complexity, which would demand writing a full class for it. Here we do not have a state class, and this is because our state is simply an integer. The code snippet inside of our main method below helps us achieve this task: Future main() async The next step you need to do is point the Hydrated BLoC library to a path where it should persist data on our local storage One of these is the latest version of the hydrated bloc library, and we add other dependencies as shown in our pubspec.yaml file below: dependencies: In order to use Hydrated BLoC for State persistence, this article assumes you have a basic understanding of using the BLoC library for state management.ĭuring the course of this project, we will need to persist our blocs, and we‘ll start by adding the necessary dependencies to help us do that. ![]() In order to demonstrate how to use persist the state of our application, we will make use of our Hydrated BLoC to ensure that whenever the app is restarted, it displays the last random number that was generated. In this tutorial, we are going to build a simple random number generator. You can simply make use of the hydrated BLoC from the BLoC library, and this is similar to onSaveInstanceState() for those that come from a native Android development background. If you are using the BLoC library for state management in Flutter, then you do not need to write a lot of code to save and restore your state. Rather than always having to browse the internet afresh, most people would love to continue from the last page they were on while using their browser application, and this is where saving the state of your application should be a huge consideration for you. This situation occurs mainly because of how our operating system tends to clear or destroy our activities and states that are contained in it, whenever we close our application.įor instance, most users would prefer using a weather application that by default, shows the weather situation of your recent location or the last location you checked, rather than having to manually search your location anytime they open it.Īnother good example of situations where state persistence is of utmost importance can be experienced when using a browser application. It makes our applications easier for users to make use of, especially when they do not have to re-enter certain data every time they launch our application. There are lots of benefits associated with properly persisting the state of our applications. Hydrated BLoC, on the other hand, is an extension of the bloc package which provides out-of-the-box persistence for our blocs or cubits. It takes in events, and based on predefined rules, yields a different state once data has been processed to meet certain criteria. The BLoC library was created to cater to state management, making it simple, powerful to use (even with large-scale business logic), and also testable at the same time.īLoC is made up of events and states. In computer sciences, this is referred to as the logic layer or business logic part of a program that encapsulates rules dictating how data can be created, stored, or modified. The acronym BloC simply means a business logic component. Persisting application state with Hydrated BLoC in FlutterīLoC is an extremely powerful solution for state management in the Flutter ecosystem. Eshiet Ekemini Follow A graduate of University of Uyo and a tech enthusiast, Ekemini has been building for mobile for two years, with a particular focus on Kotlin and Flutter.
0 Comments
Leave a Reply. |