If you’re diving into Flutter app development and are looking for an efficient way to manage your SQLite database, look no further! Floor is here to help you by abstracting SQLite functionalities with an elegant API. This guide will walk you through the essential steps to set up and use Floor in your Flutter projects.
What is Floor?
Floor is a delightful SQLite abstraction specifically designed for Flutter applications. Inspired by the Room persistence library, it provides a clear pathway for mapping in-memory objects to database rows, ensuring complete control via SQL when needed. It’s important to understand SQL and SQLite to unlock Floor’s full potential.
Key Features
- Null-safe
- Type-safe
- Reactive
- Lightweight
- SQL-centric
- No hidden magic
- No hidden costs
- Supports iOS, Android, Linux, macOS, and Windows
Setup: Getting Started with Floor
Before you can harness the power of Floor, you need to set up your Flutter project with the necessary dependencies. Follow these steps:
1. Setup Dependencies
Add the required dependencies in your pubspec.yaml
file:
dependencies:
flutter:
sdk: flutter
floor: ^1.4.2
dev_dependencies:
floor_generator: ^1.4.2
build_runner: ^2.1.2
2. Create an Entity
An entity in Floor refers to a database table and represents the business object. Use the @entity
annotation to identify the class. Make sure to include a primary key:
import 'package:floor/floor.dart';
@Entity()
class Person {
@PrimaryKey()
final int id;
final String name;
Person(this.id, this.name);
}
3. Create a DAO (Data Access Object)
A DAO manages SQLite database access. It’s an abstract class that defines method signatures for querying. Here’s how to structure your DAO:
import 'package:floor/floor.dart';
@dao
abstract class PersonDao {
@Query('SELECT * FROM Person')
Future> findAllPeople();
@Query('SELECT name FROM Person')
Stream> findAllPeopleName();
@Query('SELECT * FROM Person WHERE id = :id')
Stream findPersonById(int id);
@insert
Future insertPerson(Person person);
}
4. Create the Database
The database must extend FloorDatabase
. Ensure to annotate it with @Database()
:
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';
@Database(version: 1, entities: [Person])
abstract class AppDatabase extends FloorDatabase {
PersonDao get personDao;
}
5. Run the Code Generator
To generate the necessary code, run:
flutter packages pub run build_runner build
To watch for changes automatically, use:
flutter packages pub run build_runner watch
6. Use the Generated Code
Utilize the generated $FloorAppDatabase
class to work with the database:
final database = await $FloorAppDatabase.databaseBuilder('app_database.db').build();
final personDao = database.personDao;
final person = Person(1, 'Frank');
await personDao.insertPerson(person);
final result = await personDao.findPersonById(1);
Analogy Time: Floor as Your Home Foundation
Think of Floor as the solid foundation of your home (your application). A strong foundation (database) holds everything together. Just as you need to plan how your rooms (entities) will be laid out and accessed from the kitchen (DAO), the Floor implements the structure that enables seamless and efficient data retrieval and storage.
Troubleshooting
If you run into issues, here are some troubleshooting ideas:
- Ensure all dependencies in your
pubspec.yaml
file are correctly specified. - Check for any syntax errors in your code, especially in annotations.
- Make sure to run the code generator each time you modify entities or DAOs.
- Consult the documentation for specific issues related to SQLite or Floor functionality.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.
Additional Resources
For further examples, check out the example and test directories.