Flutter Database — SQLite and SQFlite
Introduction of Database
Database is a structure that allows us to store data in a more organized manner.
As we know different ways to store data in our Android phones such as shared preferences and writing directly to text files although all those methods that they are wonderful and useful for more complex applications. We need a more structured way to organize our data. That’s when databases come to play.
So that debate is a structured way of organizing data on top of that database also allows us to retrieve that data quicker and more efficient. Here is a representation of a database.
Take a look on above image :
As you can see to the right so you can create tables such as a person table inside of that person table. You notice we have rows and columns in case of data. That’s how the data is organized as rows and and columns. So you can see a representation of a table here. We have on the first row first column we have an ID. These IDs are incremental meaning as we add items to our table in this case the person table we can add a name you can see James and The Age column.
As you can see this is a very organized way to store data that we can later retrieve. And the other thing about databases is that we can later relate all those tables with data for a faster retrieval of that data. So all those tables we can equate relationships and we can have as many tables as we want we can have data bases with hundreds and thousands of tables.
So in a nutshell this is a representation of what a database is internally.
Database Project Setup — Dependencies and Plug
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.
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
For more details about Sqflite : https://github.com/tekartik/sqflite
For path_provider latest version : https://pub.dartlang.org/packages/path_provider