Using sembast to store data

In many cases, when you need to persist structured data in your app, you will likely choose an SQL database, such as SQFLite, which we've used in Chapter 6, Store That Data - Using Sq(F)Lite To Store Data in a Local Database. But, there are cases where your data is not structured, or it's so simple that you don't need an SQL database. For those cases, there is a very efficient solution for Flutter—the simple embedded application store database.

Sembast is a document-based database that resides in a single file. It is loaded in memory when you open it from the app, and it's very efficient, as the file is automatically compacted when needed. Data is stored in JSON format, with key–value pairs. You can even encrypt data if your app requires it.

The library is written in Dart and the only requirement that you need in order to use Sembast is the addition of the dependency in your pubspec.yaml file:

Create a new app with your editor and open the pubspec.yaml file. In the dependencies node, add the code to add the sembast and path_provider libraries. As usual, I recommend checking out the latest versions in the Dart packages website at https://pub.dev/:

sembast: ^2.3.0
path_provider: ^1.6.5

The reason why path_provider is included here is that each device saves into the file system in a different way; by using path_provider, you make sure that the app is compatible with both iOS and Android.

As usual, everything begins with a class. The first step in our project is the creation of the class for the todo itself:

  1. In the lib folder of your app, create a data folder.
  2. In the data folder, create a new file called todo.dart.
  1. In the file, add a class called Todo with the fields that we'll use in the database: id, name, description, a completeBy string that will contain the date that the task should be completed by, and an integer for the priority:
class Todo {
int id;
String name;
String description;
String completeBy;
int priority;
}
  1. To simplify the creation of a todo, create a constructor that will take all the fields (except the ID) of a todo object:
Todo(this.name, this.description, this.completeBy, this.priority);
In sembast, the ID is automatically generated from the database and is unique for each store/document, similar to what happens with SQLite.

As data is stored as JSON in sembast, we need a method to convert a Todo object into a Map; the sembast engine will then automatically convert the Map to JSON.

  1. In the Todo class, create a function called toMap() that will return a Map of the String, dynamic type containing the fields of the TodoAdd the following code to create the toMap() method:
  Map<String, dynamic> toMap() {
return {
'name': name,
'description': description,
'completeBy': completeBy,
'priority': priority,
};
}
  1. The last method of the Todo class will do exactly the opposite: when a Map is passed, the function will return a new Todo. This method is static, as it does not require an object to return a Todo. Add the following code to create the fromMap() function:
static Todo fromMap(Map<String, dynamic> map) {
return Todo(map['name'], map['description'], map['completeBy'],map['priority']);
}

This completes the Todo class. Next, let's deal with the data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.217.95