Floor In Flutter

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.

1.  Adding the Floor dependency and dev dependency

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.

2. Creating a Dao (Data as Object) class

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.

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.

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.

3. Creating the Database.

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.

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.

–> You can generate the file after adding it with the help of the following command that you have to run on the terminal:

4. Now the final step

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.

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

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*

Be the first to comment.

Start a Project

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home