The Floor Database is inspired by the Room persistence library. It comes with automatic mapping between in-memory objects and database rows while still offering full control of the database with the use of SQL.
It is a layer that sits on top of an SQLite database and makes it easier to use.
The library’s name derives from the following. Floor as the bottom layer of a Room which points to the analogy of the database layer being the bottom and foundation layer of most applications. Where fl also gives a pointer that the library is used in the Flutter context.
Read more about Flutter app development services from Mobikul
Features of Floor
- null-safe
- typesafe
- reactive
- lightweight
- SQL centric
- no hidden magic
- no hidden costs
- supports iOS, Android, Linux, macOS, Windows
Start the implementation
Step-1
Add dependencies in your pubspec.yaml file.
1 2 3 4 5 6 7 8 |
dependencies: flutter: sdk: flutter floor: ^1.2.0 dev_dependencies: floor_generator: ^1.2.0 build_runner: ^2.1.2 |
floor
holds all the code you are going to use in your application.floor_generator
includes the code for generating the database classes.build_runner
enables a concrete way of generating source code files.
Step-2
Create an entity
1) It will represent your database table
1 2 3 4 5 6 7 8 9 |
@entity class User { @primaryKey final int id; final String name; final String email; User(this.id, this.name, this.email); } |
Step-3
Create a DAO (Data Access Object)
- You can define queries by adding the
@Query
annotation to a method. The SQL statement has to get added in parenthesis. The method must return aFuture
orStream
of theEntity
you’re querying for. @insert
marks a method as an insertion method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import 'package:floor/floor.dart'; @dao abstract class UserDao { @Query('SELECT * FROM User') Future<List<User>> findAllUsers(); @Query('SELECT * FROM User WHERE id = :id') Future<Person?> findUserById(int id); @Query('DELETE FROM User WHERE id = :id') Future<void> deleteUserById(int id); @insert Future<void> insertUser(User user); } |
Step -4
It has to be an abstract class that extends FloorDatabase
. Furthermore, it’s required to add @Database()
to the signature of the class. Make sure to add the created entity to the entities
attribute of the @Database
annotation.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import 'dart:async'; import 'package:floor/floor.dart'; import 'package:sqflite/sqflite.dart' as sqflite; import 'dao/person_dao.dart'; import 'entity/person.dart'; part 'database.g.dart'; // the generated code will be there @Database(version: 1, entities: [User]) abstract class AppDatabase extends FloorDatabase { UserDao get userDao; } |
Run the Code Generator
“flutter packages pub run build_runner build” run this given command and it will solve all the errors from step 4
Step-5
Now we can use the database
1 2 3 4 5 6 7 8 |
final database = await $FloorAppDatabase.databaseBuilder('app_database.db').build(); final personDao = database.userDao; final user = User(1, 'demo', 'demo@gmail.com'); await personDao.insertUser(user); final search = await personDao.findUserById(1); final delete = await personDao.deleteUserById(1); final allUsers = await personDao.findAllUsers(); |
For more information, please go through the Flutter Dev
Hopefully, this blog will be helpful to you to understand the Floor Database. If you have any queries, please write them in the comment section.