Introduction to Drift in Flutter
We have different DBMSs in flutter such as Sqflite, Hive, Drift, etc…. Some are SQL based and some of them are NoSQL. Each of them has its advantages and disadvantages but I don’t want to discuss them here.
Drift library is used in dart and flutter programs and supports all the platforms. Developers can write queries in dart or SQL.
dependencies:
drift: ^2.0.2+1
sqlite3_flutter_libs: ^0.5.0
path_provider: ^2.0.0
path: ^1.8.2
dev_dependencies:
drift_dev: ^2.0.2
build_runner: ^2.2.0
You may think about whether all of these packages are necessary. Each of them has its usage. As I told you, Drift is our main package. It is built on top of SQLite and sqlite3_flutter_libs
is useful when you develop android or ios applications. If you’re not creating a flutter application you can not use it (at your own risk). All local databases store somewhere on the device, so it needs a location to store data, here path
and path_provider
packages help the programmer. And finally, drift_dev
and build_runner
are used for generating codes.
After installing drift, it’s time to implement tables. I used Drift package in one of my projects and now I want to share my experiences with you. Here are Category and note tables:
You can use IntColumn
, TextColum
, BoolColumn
, DateTimeColumn
, RealColumn
depend on fields of table. If you want to set a default value for a column, you can use the withDefault
function and pass the value to it. named
function is used when you want to set a different name for a field from its getter name. I think it’s clear so let’s discuss primary keys and foreign keys.
To specify primary keys one way is to override primaryKey
getter and introduce considered columns. There is reference method in drift to express foreign key(s) in your table (like categoryId
in Note table).
This method has 2 optional parameters to describe Referential Actions (onDelete
and onUpdate
). Be aware that, in sqlite3, you must enable foreign key references. They need to be enabled with PRAGMA foreign_keys = ON
. I will show you soon.
After designing tables, we need a class to manage our database and write queries. Inside @DriftDatabase
annotation, we should introduce the tables and views (if we have them) that we defined before. If we want to make special configurations when the database is being created or upgraded, we can override the migration
method. This function returns a MigrationStrategy
that receives 3 functions as input; onCreate
, onUpgrade
and beforeOpen
.
Don’t forget to run the below command to generate database.g.dart
file:
flutter pub run build_runner build
After this command is executed successfully, we have 2 versions for each of our tables; the Data version and the Companion version (for e.g NoteData
and NoteCompanion
).
Now I want to write queries. As you know we have 4 kinds of operations: insert
, update
, delete
and select
. In addition, we have where
clause, join
clause, etc. All of these can be done easily with some methods. In the following, I wrote a small example for each of them.
- The following code adds a category to the Category table. It’s simple, you just have to be careful that the
insert
function receives a aCategoryCompanion
object.
2. Updating a record is as easy as A, B, C. Drift itself considers the places where the PKs are equal and performs the update. Unlike the insert function, this function receives the CategoryData
version.
3. The next thing is about deleting a record from table. You know, to delete a record, we need a condition that we don’t delete all the records by mistake. One of the places where the where
function can stand out is exactly here to specify omissions.
4. And finally it’s time to select. The query you see in the figure below is for searching between notes. In addition, I joined the two tables as needed in my project; There are also other types of joins in drift.
An important point is that when you join two tables together, the output of your query is none of the previous two tables, in such a situation, Drift will return you a list of TypedResults
, and you have to parse the data. These events do not necessarily happen during joining tables; You may decide to add a column to your output (for example, due to the use of Aggregation functions or something else) in a query. Here too, the result needs to be parsed.
As you see, the output recognizes both tables and you can access columns separately and easily.
I hope this article would be useful for you and be aware that Drift package is more powerful than what I said. Please clap this one if it was helpful and you have learned something new.
Here is my Github repository that I used Drift there, don’t forget to star it ;)
Thanks for your attention !