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
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:
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 (
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;
Don’t forget to run the below command to generate
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
Now I want to write queries. As you know we have 4 kinds of operations:
select. In addition, we have
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
insertfunction receives a
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
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.
50+ New Flutter Interview Questions
Flutter interview questions and answers Are you a flutter developer and looking for flutter interview questions and answers? Then this article is for you. This article contains curated questions on flutter. So, make sure to read till the end. Flutter Interview questions for 1 year of experience Flutter Interview questions for 2 years of experience …
Quote Book App Flutter Tutorial – Your Step by Step Guide to Create a Beautiful Quote App
What is Flutter? In this article, we will share this flutter tutorial with you. if you are a beginner then this will help you a lot to learn flutter app development faster and easy way. Flutter is a cross-platform mobile app development framework created by Google. It allows developers to create native-looking Android and iOS …
Quote Book App Flutter Tutorial – Your Step by Step Guide to Create a Beautiful Quote App Read More »
Flutter Splash Screen Tutorial
Splash Screen is the very first screen that we see after launching an application. It is also called the launch screen. We will implement three main methods to add a splash screen to our app. So in this tutorial, we will create Flutter Splash Screen by using flutter_native_splash package. Customize Flutter’s default white native splash …