Updated 27 April 2023
Before starting our blog topic Floor In Flutter, we will discuss in short what is Floor and why we need Floor in a Flutter. The floor basically a flutter dependency that supports the RoomDataBase in the Flutter application. Those who have worked on Android application development can relate to the RoomDataBase. RoomDatabase basically provides the abstraction layer over the SQLite database. RoomDatabase consists of annotation which helps to perform the CRUD(CREATE, READ, UPDATE, and DELETE) operation easily.
The floor is inspired by the RoomDatabase of Android and has kind of same functionality and working logic as the Flutter framework. It automatically mapped the object with the row of the table, while still offering full control of the database with the use of SQL. We can store the primitive and non-primitive(Custom Object) in the Floor Database. This is all brief about the Floor Database in Flutter.
For implementing the floor in a flutter, we will follow the mentioned steps:
You can also check out our Flutter app development page.
I have used the following version dependency, you can use any version as per your requirement from the Flutter Dev console. And also add the dev dependency for the compilation of code.
1 |
floor: ^0.18.0 |
1 2 |
floor_generator: ^0.18.0 build_runner: ^1.10.3 |
We need to create an abstract class that has abstract methods of performing CRUD operation in the Database. I have stored the user name in the database, and fetching the list of the user name. You can use it according to your use.
1 2 3 4 5 |
@Query('SELECT * FROM Person') Future<List<UserDetails>> getAllUser(); @insert Future<void> insertUser(UserDetails person); |
I have created the class UserDetails in which I have added String type variable name, you can use multiple variables as per your need. I have used an annotation called entity with this class. An entity represents a table in the database, and every table has a primary key for fetching some specific data from the database.
1 2 3 4 5 6 7 8 9 10 11 |
@entity class UserDetails{ @primaryKey int id=0; String name=""; UserDetails(this.id,this.name); } |
Note: We can also store custom objects in the database, but for this, we need to create a converter for each custom object. Which will convert the custom objects to primitive data types and vice versa.
In this step, we will create the database for our model class (UserDetails in my case). We will create an abstract class that extends the FloorDatabase class.
1 2 3 4 5 6 |
@Database(version: 1, entities: [UserDetails]) abstract class UserDatabase extends FloorDatabase{ UserDetailsDao get userDao; } |
Note: Don’t forget to import the ” part ‘UserDatabase.g.dart’ ” file as this consists of all the code of the Database. In my case, my “g.dart” file name is mentioned below.
1 |
part 'UserDatabase.g.dart'; |
–> You can generate the file after adding it with the help of the following command that you have to run on the terminal:
1 2 3 4 5 |
flutter packages pub run build_runner build and whenever you change anything in the database run the following command: flutter packages pub run build_runner watch. |
Now you have generated code for saving and fetching the data from the database. I have created two methods in which I am saving the UserDetails and fetching the List of the UserDetails respectively. For obtaining an instance of the database, use the generated “$FloorUserDatabase” class, which allows access to a database builder. The name is being composed by "$Floor"
and the database class name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void addUserDetail(String name) async{ final database = await $FloorUserDatabase.databaseBuilder('floor_database.db').build(); final userDao=database.userDao; UserDetails userDetails=UserDetails(1, name); await userDao.insertPerson(userDetails); } // For fetching the UserDetails List void getUserList() async{ final database = await $FloorUserDatabase.databaseBuilder('floor_database.db').build(); final userDao=database.userDao; final result = await userDao.getAllUser(); } |
In this way, you can save the data in the database and fetch it from it. You can use Floor Database as per your need.
I hope this blog helps you to understand the Floor In Flutter.
For more information, please go through the Flutter Dev console
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.