Flutter Isar Database — The complete crash course — part 1
Isar database is a database system developed by Simon Leier for development with Flutter. However, you can also use it for Dart only, as this system is 100% written in Dart. In this article, I want to explain how you can use Isar Database to develop great apps. But before that, I’d like to introduce you to a few features:
- Highly scalable — your databases can get huge
- Asynchronous — you can send multiple queries at the same time
- Multiplatform — You can use this system not only on iOS, Android or desktop, but also on the web. Most database systems do not support web.
And a lot more. Those were the most important features for me, but you can check out all of them on the page of the package.
First you have to add
isar_flutter_libs to your dependencies.
To your dev_dependencies you have to add
to your dev_pendencies. These two packages are for generating code later.
Now it should look like this in your pubspec.yaml file:
In the first part I just want to teach you the very basic things. In the next parts we will have a closer look at the documentation and become real “database professionals” ;)
First we create a class. We annotate it with
@Collection(). We need this so that Isar can generate the code later.
Now we want to insert our first variable there. We need an id in the database to be able to identify individual entries. So that Isar recognizes this, we now create an int, which we can identify with
Our class is called Contacts. We say in our example that we assign a name and a phone number to our contact. The phone number in this example is an int.
Now we need to add a “part” declaration to add. This is important so that our code is generated correctly.
In order for our dataclass to finally be ready, we still need to run a command. Now it depends on whether you use Isar with pure Dart code or also with Flutter.
If you only use Dart, then use the following command:
dart run build_runner build.
If you use Flutter, you should run this command:
flutter pub run build_runner build.
Now you have a big file generated, but it helps Isar to manage your database.
Now, to access our database in our app, we first need to create an Isar instance.
We do this like this:
Now you will get an error. But this is not a problem. First we need to specify a schema. This is very easy to create:
Wait, we never created a class called ContactsSchema, right? Well, almost right. By generating the code, this class was created. Now Isar knows exactly how to create the database.
Now we still want to specify a directory where the database should be stored. It is set by default that for iOS
NSDocumentDirectory is used and for Android
getDataDirectory. The final location is then the path you specify. You don`t have to specify it for web.
Now we can access our database.
First we create a new contact:
To enter this contact, we call the function
Now to access all contacts we just need to call
How to get only certain entries, I will show you in the following articles.
Today you have learned the basics of Isar Database. You now know how to create classes for Isar, access the database and add new entries.
In the next articles, we will take a closer look at the database and better understand the concepts behind Isar. We will learn how to filter data, delete data and much more.
If you don’t want to miss all this, you should definitely follow me!
Thanks for reading, have a great day.
Note: This article is based on Isar’s documentation. The purpose behind it is to better understand the individual parts of the code. In today’s article, “Quickstart” was referred to. You can find the original version here: https://isar.dev/tutorials/quickstart.html