The floor provides a neat SQLite abstraction for your Flutter apps it is inspired by the Room persistence library. It comes with default mapping between memory objects and database rows while still giving complete control of the database with the use of SQL.
What is Flutter?
The Flutter framework contained both a software development kit and their widget-based user interface library. This library consists stained of various inoperable UserInterface elements, such as sliders and buttons.
Developers who create mobile applications with the Flutter framework will do so using a language called Dart. With a command like JavaScript, Dart is a typed programming language that completely focuses on front-end development.
You can save, query and, remove your Objects in a simple and direct way with the Floor database!
How do we start?
To start with Floor, let’s include these dependencies in pubspec.yaml
dependencies:
flutter:
sdk: flutter
floor: ^0.14.0
dev_dependencies:
floor_generator: ^0.14.0
build_runner: ^1.7.3
What about the entity class?
The entity class will always represent database table Columns. The @entity
adds the class as a persistent class and you need to include a primaryKey
.
// entity/student.dart
import 'package:floor/floor.dart';
@Entity(tableName: 'students')
class Student {
@primaryKey(autoGenerate: true)
final int id;
final String name;
final Float grade;
Person(this.id, this.name, this.grade);
}
What about data access objects?
This component is the most responsible for managing enable to the underlying SQLite database.
You can use the normal SQLite @Query
or @insert
, @delete and @update
.
// dao/student_dao.dart
import 'package:floor/floor.dart';
@dao
abstract class StudentDao {
@Query('SELECT * FROM students')
Future<List<Student>> findAllStudents();
@Query('SELECT * FROM students WHERE id = :id')
Stream<Student> findStudentById(int id);
@insert
Future<void> insertStudent(Student student);
@Query('DELETE FROM students WHERE id = :id')
Future<void> delete(int id);
}
What about a Database?
It has to be an abstract class that will be extends FloorDatabase
// database.dart
// required package imports
import 'dart:async';
import 'package:floor/floor.dart';
import 'package:sqflite/sqflite.dart' as sqflite;
import 'dao/student_dao.dart';
import 'entity/student.dart';
part 'database.g.dart'; // the generated code will be there
@Database(version: 1, entities: [Student])
abstract class AppDatabase extends FloorDatabase {
StudentDao get studentDao;
}
part 'database.g.dart';
make sure to add the above line and it should be equal to your database file name which in our scene is atabase.dart.
How to Build the database?
After that, go to the terminal and execute
flutter packages pub run build_runner build
make sure that you added flutter to your surrounding variables. Now you can easily access your database using :
$Floor +
your database class name which in our scene is AppDatabase
final database = await
$FloorAppDatabase.databaseBuilder('app_database.db').build();
final studentDao = database.studentDao;
final student= Student(1, 'Frank', 99);
await studentDao.insertStudent(student);
final result = await personDao.findPersonById(1);
If you want to generate the Id by default, then you can easily pass null instead of Id in the entity object, i.e:
final student= Student(null, 'Frank', 99);
await studentDao.insertStudent(student);
I hope you enjoyed this nice and easy database.
If you have any doubts about the above topic or have to get services and consultations and get flutter app development company. Feel free to contact us. AIRO GLOBAL SOFTWARE will be your strong digital partner. E-mail id: [email protected]
Author - Johnson Augustine
Chief Technical Director and Programmer
Founder: Airo Global Software Inc
LinkedIn Profile: www.linkedin.com/in/johnsontaugustine/