Flutter Sembast — The Complete Crash Course
Sembast is a NoSQL persistent store database solution for single process io applications. It’s fully document-based in a single file and is loaded in memory when opened. This can be especially useful when you want to analyze databases that the user selects. In addition, it varies a bit from other solutions, that have their “own” database system and don’t directly open
.db files, it can be very useful to learn. That’s why I will teach you in this article how to use it. Let’s get right into it!
Note: You can find the whole source code provided in this tutorial here
The first thing we have to do after creating our app is to add sembast. To do so, we will use the command
flutter pub add sembast. You should be familiar with those commands, otherwise, I recommend you to look up the basics of Flutter again.
Let’s start right away on how to open a database. A database is a single file represented by a path in the file system. That’s why we will first define a string
dbPath and another variable of type
DatabaseFactory is now used by a variable of type
Database, where we will pass the
dbPath String. That sounds complicated, but it’s very easy to understand, so let’s take a look at how it’s implemented:
Read and write records
To read and write records from the database, we create a new variable with the value
StoreRef.main(). We can now easily get and put records in there, as well as delete them. Let’s see how it works:
Normally, I would also discuss some Advanced Usage here, but because many things are very use-case specific, I will provide you links to all the things that Sembast can do, so if you want to learn them, you can just click on these links.
Further reading & Conclusion
In this article, you have learned the basics of the file-based database solution “sembast”. You have seen how easy it is to use and how useful it can be.
You can unfold the whole power of sembast if you use packages like Freezed, Isar, or Riverpod. If you want to learn these additions, I have entire tutorials about them. Check them out here.
In the following few articles, I will introduce more somewhat complicated packages and explain them. If you don’t want to miss this, I recommend you to follow me. I tried my best to write the easiest tutorial which everyone understands. If you appreciate this work, I would be very grateful if you could support this quality content and give me some claps!
Thanks for reading, have a nice day!
NOTICE: This article is based on the documentation of the Sembast package. All the information provided here is from this site and a big part of the source code is taken from the documentation. Sembast Documentation Source: https://packages/sembast and https://github.com/tekartik/sembast.dart/tree/master/sembast/doc