Flutter – SQLite Database

Tram Ho

Introduce

Perhaps people are no stranger to SQLite in mobile programming, this is a useful tool for caching data or stored locally. In Android, we are familiar with the Room library, this is a powerful library that provides a method cable to exploit the full power of SQLite most easily. On Flutter we also have a lot of plugins for working with SQLite easily such as sqlcool , sqflite , floor …. Details of the plugin you can search on pub.dev. Here I will use the floor plugin because it has many similarities with the Room library above android and it is great that this plugin supports both android and ios

Floor

Floor is a library that provides lightweight SQLite abstraction with automatic mapping between in-memory objects and database rows while providing full database control with the use of SQL.

The current version of floor is 0.9.0

Use

  1. Add necessary dependencies to the pubspec.yaml file

  • build_runner: a tool to generate needed files
  1. Create a Entity Each Entity will represent a table in our database

  • @Entity keyword: This is a table worth noting
  • @PrimaryKey keyword: This property is worthy of being the primary key of the table. This property is required. It is possible to set multiple fields as Primarykey then using @Entity (primaryKeys: [‘id’, ‘name’])
  • Keyword @ColumnInfo : to change field information like name, notnull
  1. Create a DAO

Class DAO is the component responsible for managing access to the database. The abstract class contains database query methods

  • @Query : annotation marks queries (select, delete) methods
  • @insert : annotation marks the insert data method
  • @delete : annotation marks the delete data method
  • @transaction : mark method as a transaction set consisting of one or more different transactions
  1. Create Database

Create an abstract class enxtend from FloorDatabase . Anotation @Database to mark the database version as well as the entities that need storage.

  1. Make sure you have added part ‘database.g.dart’; This file will be automatically generated when we run build_runner
  2. Run the command line flutter packages pub run build_runner build in the terminal to gen the needed files. Alternatively, you can run the command flutter packages pub run build_runner watch to automatically genize when a database or entities changes.
  3. Use generated code. To get an instance of the database, use the automatically created $ FloorAppDatabase class

app_database.db: The database name you want to set

Architecture

Querying

The query methods are marked with the @Query annotation (). When using the query, you need to ensure the accuracy of the query. The query statement can return the Future, Stream type of an Entity or Void. Few examples of query queries

Migrations

As the app version changes, the database structure changes frequently, so it is imperative that the data migrations are required to ensure the integrity of the data.

After a change in the database structure, the database version has been upgraded. Then define the changes using the implementation functions like migration1to2 …. Finally use addMigrations () when building the database.

Callback

Source: https://pub.dev/packages/floor#persisting-data-changes

Share the news now

Source : Viblo