To-do List in Flutter with SQLite as local database

Udara Abeythilake
3 min readSep 19, 2019

--

Introduction

Data is very important for users since it would be inconvenient for them to type their information every time or wait for the network to load the same data again. In situations like this, it would be better to save data locally.

In this tutorial, we’ll learn how to crate To-do list with flutter with the help of SQLite as the local database. In here we are using sqflite plugin. In the previous tutorial, I have done how to setup flutter SDK and configure.

objectives

  • Crating to-do list.
  • Handling CRUD operation to save and retrieve data.

Method

Step1

First thing first. We are going to create the model class for the to-do item. I’m using the title, description, date, and id for each to-do. Create a model class as todo.dart and insert the following code.

Step2

Next, we are going to implement the SQLite database CRUD operation. Here we are going to create a separate class and implement all insert, update delete operations by creating tables. Before that, we must add dependencies in order to use SQLite in your project. Go to your pubspec.yaml file and add the following dependencies to your project and save it.

Following are some important functions in the database_helper class.

The above function will create the database object and provide it with a getter where we will instantiate the database if it’s not. This is called lazy initialization.

If there is no object assigned to the database, we use the initializeDatabase function to create the database. In this function, we will get the path for storing the database and create the desired tables. I’m using todos as the name of the database

Next, creating the table as mention above.

Then we are going to add insert update delete functions. Below you can see the complete code for the database_helper class.

Step3

Now we need to implement the screens for the to-do list. Create a folder called Screen and add todo_list.dart file.

In the above code, we are implementing a List view. On the list view, it displayed the to-dos that we have entered. So now we are going to enter to-dos to the database. For that question below is the solution. We are creating another screen to add to-dos.

Now the application is finished. Open the terminal and run “flutter run” after you connect the mobile via USB.

Results

Conclusion

In this tutorial, you learn how to use flutter widgets and implement some app. Other than that, how to add data to the local database in a flutter. If you face any problem, please find the source code.

--

--