SQFlite Database in flutter
SQFLite is a Database plugin for flutter. It is highly reliable and embedded Database engine.For CRUD operation we are using async and await. Typically these keywords are used to write asynchronous code.
Let’s try one sample to save and retrieve employee data from SQFLite.For that ,we have to import the below dependencies to get it done.
import ‘dart:async’ - To support Asynchronous calls
import ‘dart:io’ as io’- Library allows you to work with files and directories
import ‘package:path/path.dart’ - Path package provides common operations for manipulating paths: joining, splitting, normalizing, etc
import ‘package:sqflite/sqflite.dart’- Database
import ‘package:path_provider/path_provider.dart’- Flutter plugin for finding commonly used locations on the filesystem. Supports iOS and Android.
Once we have done with flutter project creation, add the below dependencies in your pubspec.yaml
Then create a sub folder database and model in your project. After that, create a new file database.dart inside database folder and have the following code below.
Next create one model class employee.dart inside model folder
We are done with creating tables in Database. Let’s have one form with firstname, lastname, email and mobileno in HomePage
We are validating the form, when the user clicks on submit button and also we are showing the respective error through validator. After that, we are creating a instance for DBHelper and saving the employee values into database through saveEmployee()
We are half done, now we have to create a one more page to list all the saved employees . It will show CircularProgressBar until it fetch the data from database, if there is no data it will show the text No data found.
Thanks for Reading :-)
For complete reference, checkout this link sqflitesample