In the Mobile devices, there are several ways to store persistent data. Flutter and Dart don't provide a built-in abstraction for accessing SQLite databases.
.
But Flutter supports an open source module SQFlite. SQFlite is a way of storing app data in Flutter Application. SQFlite is a Database plugin for flutter. It is highly reliable and embedded Database engine. All the CRUD operation in the database will run on a background thread. SQFlite support transactions batches and automatic version management during open.
.
But Flutter supports an open source module SQFlite. SQFlite is a way of storing app data in Flutter Application. SQFlite is a Database plugin for flutter. It is highly reliable and embedded Database engine. All the CRUD operation in the database will run on a background thread. SQFlite support transactions batches and automatic version management during open.
In this article, we are going to learn the basics of SQFlite database with a real-time example.
In this example, we are going to create a user database and we'll perform save, retrieve, update and delete operation. As you can see above, the first screen of app showing list of store user information. We have implemented dialog for add and edit stored user information.
So, let's start the implementation of SQFlite database. At the end, the final project structure looks like :
here, we have a singleton instance of the database and CRUD operation of the project. It will act as our data access object as well by providing functions to query for specific data models.
here, we have provided our home screen widget instance.
6. After that create home screen widget hierarchy. Here, we have added a title and an add new user button in AppBar. To display the list of users, we have used FutureBuilder.
It'll display a progress bar until we loading user list from the database.
In this example, we are going to create a user database and we'll perform save, retrieve, update and delete operation. As you can see above, the first screen of app showing list of store user information. We have implemented dialog for add and edit stored user information.
So, let's start the implementation of SQFlite database. At the end, the final project structure looks like :
1. user.dart is a POJO and table for the database.
2. database_hepler.dart is our database utility class. Here, we handle our project CRUD operations.
3. add_user_dialog.dart is a dialog. With the help of this user interface, we can store and edit the database.
4. home_presenter.dart will maintain a business layer of the app.
5. homescreen.dart will display the first screen of the project.
6. list.dart will show a list of users that is stored in the database.
7. main.dart is the entry point of the app.
Create a new project.
1. Let's start it and create a new Flutter project from New->new Flutter Project. I'm assuming, you have set up development tool and you have basic knowledge of flutter application development. If not, do it here.
2. Now modify project dependency pubspec.yaml.
- sqflite: "^0.11.0" is SQFlite is a Database plugin for flutter. It'll handle all CRUD operations.
- path_provider: ^0.3.1 plugin allows us to access the user directories on iOS and Android. Where we have stored the SQFlite database file.
3. Then create a subfolder database and model in your project as shown above. After that, create a new file database_hepler.dart inside database folder and have the following code below.
here, we have a singleton instance of the database and CRUD operation of the project. It will act as our data access object as well by providing functions to query for specific data models.
4. After that create a model dart file user.dart in the model subfolder of the database. It'll be a table in our database.
we'll use it for database operation and display list of stored user in the database.
5. Now, move to the user interface part and create main.dart. In this class initializes the theme of the app. as shown below.
we'll use it for database operation and display list of stored user in the database.
5. Now, move to the user interface part and create main.dart. In this class initializes the theme of the app. as shown below.
6. After that create home screen widget hierarchy. Here, we have added a title and an add new user button in AppBar. To display the list of users, we have used FutureBuilder.
7. For project simplicity, we using here MVP architecture. So create a presenter home_presenter.dart. Here, We have written, all CRUD operations that'll invoke from home or dialog widget.
If you have followed the article carefully, you can see the app running very smoothly as shown in the video demo. You can get working project source code of Flutter SQFlite database on Github.
But if you facing any problem, please feel free to ask in the comment section below.
Learn Flutter SQlite datebase from Expert- Udemy
8. For perform edit and add a new user entry in the database. We have created a dialog. If you facing any issue to implement dialog in Flutter. please read it here in detail.
Here, we have used textField widget for entering user details. Once, you tab on add or edit. All information will be stored in the database.
9. At the end create user list widget where we'll display store user list.
If you have followed the article carefully, you can see the app running very smoothly as shown in the video demo. You can get working project source code of Flutter SQFlite database on Github.
But if you facing any problem, please feel free to ask in the comment section below.
Learn Flutter SQlite datebase from Expert- Udemy