There are a number of tutorials out there about how to setup a SQLite database in Flutter using the SQFlite plugin. They were helpful but I found myself getting lost in the details. My purpose in this post is to give you a minimal example that you can get through in 10 minutes. Once you have it working, you can go on to some of the more advanced topics in other tutorials.
Here is the minimal app that we will be making:
Pressing each button will perform the related operation on the database using SQFlite.
For this minimal example, you will do the following steps:
- Add the dependencies
- Make a database helper class
- Make your app layout with the button logic
Start by creating a new Flutter project called
Open the pubspec.yaml file and add the following lines to the dependencies section:
Create a new file in the
lib folder and call it
Copy in the following code:
Open your main.dart file and replace it with the following code:
That’s it! You should be able to run the app now. Pressed the buttons an watch the logged output from the print statements. (In Android Studio make sure you have the Run tab selected.)
Read back through the code comments to understand what the different parts do. If there is anything that you don’t understand, you can ask in a comment below.