![]() ![]() To be able to serialize it, we need to extend the CalculationModel:Ģ 3 import 'package:flutter/material.dart' Ĥ import 'package:shared_preferences/shared_preferences.dart' ĩ static String _sharedPreferenceKey = 'calculation_history' ġ4 15 SharedPreferences sharedPreferences ġ9 20 if ( !sharedPreferences. This way, we can just store and load a string that represents the list of calculations. That’s why we need to serialize the data we want to store. The shared preferences package only supports primitive data types. We know both of the functionalities this service should have: storing a calculation (adding it to the existing history) and fetching the existing history. The resulting call hierarchy is supposed to look like this: Flutter BLoC service layer call hierarchy Implementationįirst thing we are going to do is implementing the service. Since these calculations should persist over multiple app starts, we use the shared_preferences to store them. Let’s extend it by a history of executed calculations. What we have so far is a calculator that enables the user to execute basic operations. The final calculator displaying its history Theory Its creation was documented in this tutorial, applying the BLoC pattern in this tutorial. This is necessary to enable our widget to communicate with the BLoC. The project this tutorial is based on is the basic calculator whose code can be found here. First off, in the main.dart, we need to add a BlocProvider. This also serves the purpose of increasing the level of comprehensibility as it’s not only theory. In order for this tutorial to mainly focus on the connection to the service, we use an already existing project to start from. ![]() This tutorials aims for an understandable explanation on how to add a service dependency to a BLoC. As soon as other components such as services are involved, things get a little bit more complicated. ![]() A BLoC that only processes events from a widget and emits states based on its own logic is fairly simple once you know the concept. ![]()
0 Comments
Leave a Reply. |