Use Local SQL with Dart Drift — The Crash Course

Use Local SQL with Dart Drift — The Crash Course

Drift is a reactive persistence database library, that is built on top of SQLite. It has many features and I can recommend everyone to learn it. That’s why I will show you how to use Drift in Dart & Flutter today! Let’s get started!

Happy reading

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 drift. To do so, we will use the command flutter pub add drift, flutter pub add sqlite3_flutter_libs, flutter pub add path_provider, flutter pub add path, flutter pub add build_runner -d and flutter pub add drift_dev -d. You should be familiar with those commands, otherwise, I recommend you to look up the basics of Flutter again.

But what does each of these packages do?

*drift*: This is the core package defining most apis

*sqlite3_flutter_libs*: Ships the latest *sqlite3* version with your Android or iOS app. This is not required when you're not using Flutter, but then you need to take care of including *sqlite3* yourself. For an overview on other platforms, see platforms.

*path_provider* and *path*: Used to find a suitable location to store the database. Maintained by the Flutter and Dart team

*drift_dev*: This development-only dependency generates query code based on your tables. It will not be included in your final app.

*build_runner*: Common tool for code-generation, maintained by the Dart team


Create Tables & Databases

First, we have to create our tables and databases. One SQL Database can have multiple tables and each table has different columns. We will follow the tutorial provided in the official drift documentation. First, we will create our two tables:

Create Tables

The first thing we have to do is create a file. We will name it drift_tutorial.dart to make it easy. You can name it after the database you create. After doing so, we import drift and make this file a part of drift_tutorial.g.dart. This file doesn’t exist yet and will raise an error, but we will fix that later (Don’t create this file manually).

After doing this, we will create a class called Todos (This will be the table name) that extends Table. Inside this class, you can create different columns. You can specify many things and it’s quite hard to explain it without showing any code, so I will just show you the code and you will understand what’s happening there for sure!

After creating our first table, we now want to create our second one, called Categories. But that’s not a good name for a table, because Drift recognizes, that it’s written in the plural and would create a table called Categorie. That’s why we will annotate the class with DataClassName('Category').

Great, we now know how to create tables. But the tables are useless if we don’t have any database where we store them. So let’s take a look at this now!

Create database

Creating the database where our tables will be stored is pretty straightforward. We create a class, in this example TutorialDatabase that will be annotated with DriftDatabase. We will provide an argument tables there, that takes a list of classes that extend from Table. So we will provide tables: [Todos, Categories]. In addition, the class will extend _$<Your Class Name>, so in our example _$TutorialDatabase. Also, this class does not exist yet, but we will fix that soon.

Great! Now let’s open a connection to a real file. To do so, we first have to create a constructor that calls our function _openConnection(). We haven’t created this function yet, but it will tell the database where to store the data. In addition to that, we create an int schemeVersion. You should change this number as soon as you change or add a table definition.

Outside of this class, we now create a function called _openConnection that returns LazyDatabase. In that, you have to provide a function, where we first call getApplicationDocumentsDirecoty() to get the documents directory of the application, create a variable file that has as a value **File**, and then we will return NativeDatabase where we pass our file. This sounds very complicated, but is very easy if we look at the code:

Modifying the database

Okay, now let’s get, watch, insert, update and delete todos. This is very easy to do. We just go back to our TutorialDatabase class and write 5 functions there: getAllTodos, watchAllTodos, insertTodo, updateTodo, deleteTodo. You will see in the following example, that it’s very easy to write the logic for these functions:

That is very easy, right?

Fix All the Errors

I’ve always said that we will fix the error later. And that moment has come now. Everything you need to do is to navigate to your project in the terminal and write flutter pub run build_runner build --delete-conflicting-outputs. Now, a file will be created that should fix all your errors. You have to rerun this command every time you add a column to the tables, edit the database class, or do something else in this file.

Further reading & Conclusion

In this article, you have learned the basics of the local database solution “drift”. You have seen how easy it is to use and how easy it is to use SQL in Dart.

You can unfold the whole power of drift if you use packages like Freezed, Flutter Hooks, 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 follow me. I tried my best to write the most straightforward 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 Drift package. All the information provided here is from this site and a big part of the source code is taken from the documentation and maybe modified a little bit. Drift Documentation Source:

Did you find this article valuable?

Support Tomic Riedel by becoming a sponsor. Any amount is appreciated!