Best Flutter CRUD Tutorial Using Drift Package

We have different DBMS like Sqflite, Hive, Drift, etc. Some are SQL-based, and some are NoSQL-based. Each of them has its pros and cons, but I don’t want to talk about them here.

Flutter Drift Package Step-to-Step Implementation Guide

The Drift Package is used in both Dart and Flutter applications and supports all platforms. Developers can write queries in Dart or SQL.

The best part of this package is its well-organized documentation which anyone can understand.

Adding The Dependencies

Add the following to your pubspec.yaml:

How to define a Table using drift

You may wonder if all these packages are necessary. Each of them has its own use. As I said, the Drift package is our core package. Drift is built on SQLite and sqlite3_flutter_libs helps to develop Android or iOS apps. If you don’t create a Flutter app, you may not use it (at your own risk). All local databases are stored somewhere on the device, so you need a place to store the data, where the path and path_provider packages help the developer. Finally, drift_devand is used to generate the code build_runnera.

Flutter CRUD Tutorial

After installing Drift package, it’s time to put the tables into action. I have used the drift package in one of my projects and now I want to share my experience with you. Here are Category and note tables:

Drift Package

Depending on the table field, IntColumn, TextColumn, BoolColumn, DateTimeColumn and RealColumn can be used. If you want to set a default value for a column, you can use the withDefault function and pass it a value. The named functions are used when you want to set a different name for a field from its getter name. I think it goes without saying, so let’s talk about primary keys and foreign keys.

To specify primary keys one way is to override primaryKey getter and introduce considered columns. There is a 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 (onDeleteand 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 @DrfitDatabase 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 migrationmethod. This function returns a MigrationStrategy that receives 3 functions as input; onCreateonUpgrade 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: insertupdatedelete 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.

  1. 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 CategoryCompanionobject.

2. Updating a record is as easy as A, B, and C. Drift package 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 a 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.