Persist data with Sembast NoSQL database in flutter.

Hrishikesh Deshmukh
4 min readJan 4, 2020

In this post let’s switch from a traditional way of storing data into SQLite Database to storing data in a NoSQL Database.

NoSQL database does not require a structured schema that defines each table and the related columns. This provides a much more flexible approach to storing data than a relational database, such as SQLite. which basically means, that we wont be creating any tables here, simply pass the objects into a Storage.

We can store data in a NoSQL database in flutter locally on user device as well as cloud(Goodle FireStore).

WHAT is Sembast?

SEMBAST => Simple Embedded Application Store database.

Sembast if yet another NoSQL persistent store database solution for single process I/O applications. The whole document based database resides in a single file and is loaded in memory when opened. Changes are appended right away to the file and the file is automatically compacted when needed.

Encryption of database file is also supported in sembast, which can therefore be a good option for app developers to give it a try.

How Does Sembast stores the database rows/records?

Well sembast uses a concept of Store, which we can think of a table that has a record and a key associated to it. The key is generated automatically and is unique for each record. So, there is no need of having an entity that needs to be assigned as AUTOINCREMENT.

Each record in a store shall look like this:

{“key”:1, ”store”:”StoreName”, ”value”:{}}

key: This is the entity that is auto-generated with every record and is unique.

store: Store can be thought of as an equivalent to a table in relational database.It identifies the table/folder to which the particular data belongs to.

value: value is nothing but the actual data that shall reside in the database table and it should stored in key value pair, as sembast is a NoSQL database.

We will try to deal with a simple student scenario, and try to insert a student and then fetch all the records from the sembast database.

Enough of talk, let’s dive in to Code:
GO ahead and create a new flutter project.We will need some dependencies to work with sembast:

path_provider: ^1.5.1
sembast: ^2.1.2+2

Create a new Dart file, that describes a student model and add its toJson() and fromJson() methods.

Check out this cool and awesome Quick-type tool to create your student model.

StudentModel.dart

Now Let us setup our database in another dart file.

DatabaseSetup.dart

Now, let us make a StudentDAO to deal with the students in the database.

Student_DAO.dart

Let us understand the code mentioned above:

Firstly we stores the name of our table/folder in a String variable and then used a intMapStoreFactory.store(folderName) to initialise it as a table/folder.

Line 10 : get the instance of Students Database.

Line 12 : implements the insert student, in which we use add() method associated to the folder/table name(_studentFolder in this case) and pass the database instance and the student model in Map format to be inserted into the database.

Line 18: implements the function, in which we use update() method to update the student record, using a finder object. The finder object is nothing but a key or a parameter that is passed to the update function, on whose basis, your records will be updated. It is an equivalent to whereArgs: property in SQLite.

Line 25 : implements the delete function, which has same procedure as update method. The only difference is the name of method. We use delete() method.

Line 30 : implements the getAllStudents() function in which we will get all the records of our students residing in your database and return a list of our records.

Time to make our UI and call the methods to get Students and insert our Students.

In your main file, make the necessary text-fields, action buttons and the functions, that will take the user data and invoke the insert() and getAllStudents() function in StudentDao class.

Mine looks like this:

Go ahead and run your application to see the Sembast db in action !!!

Student Insertion

For every successful insert, you shall get the message of successful student insertion in your output console, just so like this.

Successful insertion

Now, we click our next button, to get the 4 students, to see out 4 students into our console.

Successful retrieval of students.

Console output for the student data

If you have liked this post, go ahead and give it a clap, share it with your friends.For more posts, please hit the subscribe button.For any suggestions or questions please let me know in the comments section. Until next time…#HappyLearning and #HappyCoding !!!

--

--