floor 0.12.0

  • Readme
  • Changelog
  • Installing
  • 94

Floor #

A supportive SQLite abstraction for your Flutter applications (iOS, Android and macOS)

The Floor library provides a lightweight SQLite abstraction with automatic mapping between in-memory objects and database rows while still offering full control of the database with the use of SQL.

This package is still in an early phase and the API will likely change.

pub package build status codecov

Table of Contents #

  1. Quick Start
  2. Architecture
  3. Entities
    1. Supported Types
    2. Foreign Keys
    3. Primary Keys
    4. Indices
    5. Ignoring Fields
  4. Database Views
  5. Data Access Objects
    1. Queries
    2. Data Changes
    3. Streams
    4. Transactions
  6. Migrations
  7. In-Memory Database
  8. Initialization Callback
  9. Testing
  10. Examples
  11. Snapshot Version
  12. Naming
  13. Bugs and Feedback
  14. License

Quick Start #

  1. Add the runtime dependency floor as well as the generator floor_generator to your pubspec.yaml. The third dependency is build_runner which has to be included as a dev dependency just like the generator.

    • 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.

         sdk: flutter
       floor: ^0.12.0
       floor_generator: ^0.12.0
       build_runner: ^1.7.3
  2. Create an Entity

    It will represent a database table as well as the scaffold of your business object. @entity marks the class as a persistent class. It's required to add a primary key to your table. You can do so by adding the @primaryKey annotation to an int property. There is no restriction on where you put the file containing the entity.

     // entity/person.dart
     import 'package:floor/floor.dart';
     class Person {
       final int id;
       final String name;
       Person(this.id, this.name);
  3. Create a DAO (Data Access Object)

    This component is responsible for managing access to the underlying SQLite database. The abstract class contains the method signatures for querying the database which have to return a Future or Stream.

    • 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 a Future or Stream of the Entity you're querying for.

    • @insert marks a method as an insertion method.

     // dao/person_dao.dart
     import 'package:floor/floor.dart';
     abstract class PersonDao {
       @Query('SELECT * FROM Person')
       Future<List<Person>> findAllPersons();
       @Query('SELECT * FROM Person WHERE id = :id')
       Stream<Person> findPersonById(int id);
       Future<void> insertPerson(Person person);
  4. Create the Database

    It has to be an abstract class which 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.

    In order to make the generated code work, it's required to also add the listed imports.

     // database.dart
     // required package imports
     import 'dart:async';
     import 'package:floor/floor.dart';
     import 'package:path/path.dart';
     import 'package:sqflite/sqflite.dart' as sqflite;
     import 'dao/person_dao.dart';
     import 'model/person.dart';
     part 'database.g.dart'; // the generated code will be there
     @Database(version: 1, entities: [Person])
     abstract class AppDatabase extends FloorDatabase {
       PersonDao get personDao;
  5. Make sure to add part 'database.g.dart'; beneath the imports of this file. It's important to note that 'database' has to get exchanged with the filename of the database definition. In this case, the file is named database.dart.

  6. Run the generator with flutter packages pub run build_runner build. To automatically run it, whenever a file changes, use flutter packages pub run build_runner watch.

  7. Use the generated code. For obtaining an instance of the database, use the generated $FloorAppDatabase class, which allows access to a database builder. The name is composited from $Floor and the database class name. The string passed to databaseBuilder() will be the database file name. For initializing the database, call build().

     final database = await $FloorAppDatabase.databaseBuilder('app_database.db').build();
     final person = await database.findPersonById(1);
     await database.insertPerson(person);

For further examples take a look at the example and floor_test directories.

Architecture #

The components for storing and accessing data are Entity, Data Access Object (DAO) and Database.

The first, Entity, represents a persistent class and thus a database table. DAOs manage the access to Entities and take care of the mapping between in-memory objects and table rows. Lastly, Database, is the central access point to the underlying SQLite database. It holds the DAOs and, beyond that, takes care of initializing the database and its schema. Room serves as the source of inspiration for this composition, because it allows creating a clean separation of the component's responsibilities.

The figure shows the relationship between Entity, DAO and Database.

Floor Architecture

Entities #

An entity is a persistent class. Floor automatically creates the mappings between the in-memory objects and database table rows. It's possible to supply custom metadata to Floor by adding optional values to the Entity annotation. It has the additional attribute of tableName which opens up the possibility to use a custom name for that specific entity instead of using the class name. foreignKeys allows adding foreign keys to the entity. More information on how to use these can be found in the Foreign Keys section. Indices are supported as well. They can be used by adding an Index to the indices value of the entity. For further information of these, please refer to the Indices section.

@PrimaryKey marks property of a class as the primary key column. This property has to be of type int. The value can be automatically generated by SQLite when autoGenerate is enabled. For more information about primary keys and especially compound primary keys, refer to the Primary Keys section.

@ColumnInfo enables custom mapping of single table columns. With the annotation, it's possible to give columns a custom name and define if the column is able to store null.

@Entity(tableName: 'person')
class Person {
  @PrimaryKey(autoGenerate: true)
  final int id;

  @ColumnInfo(name: 'custom_name', nullable: false)
  final String name;

  Person(this.id, this.name);

Supported Types #

Floor entities can hold values of the following Dart types which map to their corresponding SQLite types and vice versa.

  • int - REAL
  • double - REAL
  • String - TEXT
  • bool - REAL (0 = false, 1 = true)
  • Uint8List - BLOB

Primary Keys #

Whenever a compound primary key is required (e.g. n-m relationships), the syntax for setting the keys differs from the previously mentioned way of setting primary keys. Instead of annotating a field with @PrimaryKey, the @Entity annotation's primaryKey attribute is used. It accepts a list of column names that make up the compound primary key.

@Entity(primaryKeys: ['id', 'name'])
class Person {
  final int id;

  final String name;

  Person(this.id, this.name);

Foreign Keys #

Add a list of ForeignKeys to the Entity annotation of the referencing entity. childColumns define the columns of the current entity, whereas parentColumns define the columns of the parent entity. Foreign key actions can get triggered after defining them for the onUpdate and onDelete properties.

  tableName: 'dog',
  foreignKeys: [
      childColumns: ['owner_id'],
      parentColumns: ['id'],
      entity: Person,
class Dog {
  final int id;

  final String name;

  @ColumnInfo(name: 'owner_id')
  final int ownerId;

  Dog(this.id, this.name, this.ownerId);

Indices #

Indices help speeding up query, join and grouping operations. For more information on SQLite indices please refer to the official documentation. To create an index with floor, add a list of indices to the @Entity annotation. The example below shows how to create an index on the custom_name column of the entity.

The index, moreover, can be named by using its name attribute. To set an index to be unique, use the unique attribute.

@Entity(tableName: 'person', indices: [Index(value: ['custom_name'])])
class Person {
  final int id;

  @ColumnInfo(name: 'custom_name', nullable: false)
  final String name;

  Person(this.id, this.name);

Ignoring Fields #

Getters, setters and all static fields of entities are ignored by default and thus excluded from the library's mapping. In case further fields should be ignored, the @ignore annotation should be used and applied as shown in the following snippet.

class Person {
  final int id;

  final String name;

  String nickname;

  // ignored by default
  String get combinedName => "$name ($nickname)";

  Person(this.id, this.name);

Database Views #

If you want to define static SELECT-statements which return different types than your entities, your best option is to use @DatabaseView. A database view can be understood as a virtual table, which can be queried like a real table.

A database view in floor is defined and used similarly to entities, with the main difference being that access is read-only, which means that update, insert and delete functions are not possible. Similarly to entities, the class name is used if no viewName was set.

@DatabaseView('SELECT distinct(name) AS name FROM person', viewName: 'name')
class Name {
  final String name;


Database views do not have any foreign/primary keys or indices. Instead, you should manually define indices which fit to your statement and put them into the @Entity annotation of the involved entities.

Setters, getters and static fields are automatically ignored (like in entities), you can specify additional fields to ignore by annotating them with @ignore.

After defining a database view in your code, you have to add it to your database by adding it to the views field of the @Database annotation:

@Database(version: 1, entities: [Person], views:[Name])
abstract class AppDatabase extends FloorDatabase {
  // DAO getters

You can then query the view via a DAO function like an entity.

NOTE: Be aware that it is currently not possible to return a Stream object from a function which queries a database view.

Data Access Objects #

These components are responsible for managing access to the underlying SQLite database and are defined as abstract classes with method signatures and query statements. DAO classes can use inherited methods by implementing and extending classes while also using mixins.

abstract class PersonDao {
  @Query('SELECT * FROM Person')
  Future<List<Person>> findAllPersons();

  @Query('SELECT * FROM Person WHERE id = :id')
  Stream<Person> findPersonById(int id);

  Future<void> insertPerson(Person person);

Queries #

Method signatures turn into query methods by adding the @Query() annotation with the query in parenthesis to them. Be patient about the correctness of your SQL statements. They are only partly validated while generating the code. These queries have to return either a Future or a Stream of an entity or void. Returning Future<void> comes in handy whenever you want to delete the full content of a table, for instance. Some query method examples can be seen in the following.

@Query('SELECT * FROM Person WHERE id = :id')
Future<Person> findPersonById(int id);

@Query('SELECT * FROM Person WHERE id = :id AND name = :name')
Future<Person> findPersonByIdAndName(int id, String name);

@Query('SELECT * FROM Person')
Future<List<Person>> findAllPersons(); // select multiple items

@Query('SELECT * FROM Person')
Stream<List<Person>> findAllPersonsAsStream(); // stream return

@Query('DELETE FROM Person')
Future<void> deleteAllPersons(); // query without returning an entity

@Query('SELECT * FROM Person WHERE id IN (:ids)')
Future<List<Person>> findPersonsWithIds(List<int> ids); // query with IN clause

Query arguments, when using SQLite's LIKE operator, have to be supplied by the input of a method. It's not possible to define a pattern matching argument like %foo% in the query itself.

// dao
@Query('SELECT * FROM Person WHERE name LIKE :name')
Future<List<City>> findPersonsWithNamesLike(String name);

// usage
final name = '%foo%';
await dao.findPersonsWithNamesLike(name);

Data Changes #

Use the @insert, @update and @delete annotations for inserting and changing persistent data. All these methods accept single or multiple entity instances.

@insert marks a method as an insertion method. When using the capitalized @Insert you can specify a conflict strategy. Else it just defaults to aborting the insert. These methods can return a Future of either void, int or List<int>.

  • void return nothing
  • int return primary key of inserted item
  • List<int> return primary keys of inserted items

@update marks a method as an update method. When using the capitalized @Update you can specify a conflict strategy. Else it just defaults to aborting the update. These methods can return a Future of either void or int.

  • void return nothing
  • int return number of changed rows

@delete marks a method as a deletion method. These methods can return a Future of either void or int.

  • void return nothing
  • int return number of deleted rows
// examples of changing multiple items with return

Future<List<int>> insertPersons(List<Person> person);

Future<int> updatePersons(List<Person> person);

Future<int> deletePersons(List<Person> person);

Streams #

As already mentioned, queries can not only return a value once when called but also a continuous stream of query results. The returned stream keeps you in sync with the changes happening to the database table. This feature plays really well with the StreamBuilder widget.

These methods return a broadcast stream. Thus, it can have multiple listeners.

// definition
@Query('SELECT * FROM Person')
Stream<List<Person>> findAllPersonsAsStream();

// usage
  stream: dao.findAllPersonsAsStream(),
  builder: (BuildContext context, AsyncSnapshot<List<Person>> snapshot) {
    // do something with the values here

NOTE: It is currently not possible to return a Stream if the function queries a database view. This is mostly due to the complexity of detecting which entities are involved in a database view.

Transactions #

Whenever you want to perform some operations in a transaction you have to add the @transaction annotation to the method. It's also required to add the async modifier. These methods can only return Future<void>.

Future<void> replacePersons(List<Person> persons) async {
  await deleteAllPersons();
  await insertPersons(persons);

Migrations #

Whenever you are doing changes to your entities, you're required to also migrate the old data.

First, update your entity. Next, Increase the database version. Define a Migration which specifies a startVersion, an endVersion and a function that executes SQL to migrate the data. At last, use addMigrations() on the obtained database builder to add migrations. Don't forget to trigger the code generator again, to create the code for handling the new entity.

// update entity with new 'nickname' field
@Entity(tableName: 'person')
class Person {
  @PrimaryKey(autoGenerate: true)
  final int id;

  @ColumnInfo(name: 'custom_name', nullable: false)
  final String name;

  final String nickname;

  Person(this.id, this.name, this.nickname);

// bump up database version
@Database(version: 2)
abstract class AppDatabase extends FloorDatabase {
  PersonDao get personDao;

// create migration
final migration1to2 = Migration(1, 2, (database) async {
  await database.execute('ALTER TABLE person ADD COLUMN nickname TEXT');

final database = await $FloorAppDatabase

In-Memory Database #

To instantiate an in-memory database, use the static inMemoryDatabaseBuilder() method of the generated $FloorAppDatabase class instead of databaseBuilder().

final database = await $FloorAppDatabase.inMemoryDatabaseBuilder().build();

Initialization Callback #

In order to hook into Floor's database initialization process, Callback should be used. It allows the invocation of three separate callbacks which are triggered when the database has been

  • initialized for the first time (onCreate).
  • opened (onOpen).
  • upgraded (onUpgrade).

Each callback is optional.

Their usage can be seen in the following snippet.

final callback = Callback(
   onCreate: (database, version) { /* database has been created */ },
   onOpen: (database) { /* database has been opened */ },
   onUpgrade: (database, startVersion, endVersion) { /* database has been upgraded */ },

final database = await $FloorAppDatabase

Testing #

In order to run database tests on your development machine without the need to deploy the code to an actual device, the setup has to be configured as shown in the following. For more test references, check out the project's tests.

In case you're running Linux, make sure to have sqlite3 and libsqlite3-dev installed.


    sdk: flutter
  floor: ^0.12.0

  floor_generator: ^0.12.0
  build_runner: ^1.7.3

  # additional dependencies
    sdk: flutter
      url: git://github.com/tekartik/sqflite_more
      ref: dart2
      path: sqflite_ffi_test


import 'package:floor/floor.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:matcher/matcher.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_ffi_test/sqflite_ffi_test.dart';

// your imports follow here
import 'dao/person_dao.dart';
import 'database.dart';
import 'model/person.dart';

void main() {

  group('database tests', () {
    TestDatabase database;
    PersonDao personDao;

    setUp(() async {
      database = await $FloorTestDatabase
      personDao = database.personDao;

    tearDown(() async {
      await database.close();

    test('find person by id', () async {
      final person = Person(1, 'Simon');
      await personDao.insertPerson(person);

      final actual = await personDao.findPersonById(person.id);

      expect(actual, equals(person));

Examples #

For further examples take a look at the example and test directories.

Snapshot Version #

In case you want to play with the most recent changes that haven't been released to pub yet, apply following changes to your pubspec.yaml.

    sdk: flutter
      url: git://github.com/vitusortner/floor.git
      ref: develop
      path: floor

      url: git://github.com/vitusortner/floor.git
      ref: develop
      path: floor_generator
  build_runner: ^1.7.3

Naming #

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.

Bugs and Feedback #

For bugs, questions and discussions please use Github Issues. For general communication use floor's Slack.

License #

Copyright 2019 Vitus Ortner

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at


Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.

Changelog #

0.12.0 #

Changes #

  • Ignore Getters&Setters
  • Use Flutter bundled pub to get and upgrade project dependencies
  • Generate database implementation on every CI run
  • Throw exception when querying for unsupported type
  • Add generated code for example app
  • Add workflow scripts
  • Run real database tests on development machine and CI

🚀 Features #

  • Support ByteArrays/Blobs
  • Support inherited fields for entities and views
  • Support database views
  • Support inherited DAO methods
  • Support asynchronous migrations

🐛 Bug Fixes #

  • Fix failing SQLite installation process on CI
  • Fix failing stream query test

0.11.0 #

Changes #

  • Refactor string utility function into extension function
  • Refactor annotation check functions to use extension functions
  • Refactor type check functions to use extension functions

🚀 Features #

  • Ignore fields of entities by adding ignore annotation
  • Handle named constructor parameters and ignore field order
  • Exclude static fields from entity mapping

0.10.0 #

Changes #

  • Update dependencies
  • Update README with correct instructions to initialize in memory database

🐛 Bug Fixes #

  • Make in-memory database actually be just in memory

0.9.0 #

🐛 Bug Fixes #

  • Make IN clauses work with strings
  • Fix foreign key action string representation

0.8.0 #

Changes #

  • Update README with clear package import instructions

🚀 Features #

  • Introduce static 'to map' functions
  • Add optional callback functions when opening database

🐛 Bug Fixes #

  • Allow int and string (composite) primary keys

0.7.0 #

🐛 Bug Fixes #

  • Retain reactivity when using transactions

0.6.0 #

🚀 Features #

  • Add support for IN clauses in query statements
  • Enable compound primary keys

0.5.0 #

Changes #

  • Make tasks deletable in example app

🚀 Features #

  • Allow multiline string queries
  • Allow void-return queries with arguments

0.4.2 #

🐛 Bug Fixes #

  • Fix query parameter substitution regex

0.4.0 #

Changes #

  • Enable coverage report
  • Simplify type assertions and add tests

🚀 Features #

  • Allow more convenient database initialization

🐛 Bug Fixes #

  • Use query argument binding instead of manual binding

0.3.0 #

Changes #

  • Use TypeChecker for all annotations
  • Add publishing instructions
  • Remove unused annotation names
  • Simplify the mapping from an entity to a map
  • Fix database writer test
  • Make stream emit query result on subscription
  • Update example to use StreamBuilder
  • Update README

🐛 Bug Fixes #

  • Correct mapper instance name referenced by generated query methods
  • Fix adapter instances naming

0.2.0 #

Changes #

  • Add database adapters
  • Run floor Flutter tests
  • Move value objects to value_objects directory
  • Map source elements into value objects in processors
  • Use GeneratorForAnnotation and TypeChecker to verify annotations
  • Throw more specific errors on obfuscated database annotation

🚀 Features #

  • Add support for migrations
  • Add support for returning Streams as query result
  • Support accessing data from Data Access Objects
  • Add entity classes to database annotation
  • Add support for indices

0.1.0 #

🚀 Features #

  • Support conflict strategies when inserting and updating records
  • Add support for running queries that return void
  • Add support for foreign keys
  • Add parameter verification for query methods
  • Return deleted row count on delete
  • Return updated rows count on update
  • Return ID/s of inserted item/s
  • Add support for transactions
  • Add support for changing (insert, update, delete) lists
  • Support custom entity name
  • Enable NOT NULL columns
  • Enable custom column name mapping
  • Add delete methods code generation and fix update methods
  • Add update methods code generation
  • Add insert methods code generation
  • Add code generator for query methods
  • Code generation for database creation

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  floor: ^0.12.0

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:floor/floor.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Apr 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and floor.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
floor_annotation ^0.7.0 0.7.0
flutter 0.0.0
meta ^1.1.8 1.1.8
path ^1.6.4 1.6.4
sqflite ^1.2.0 1.3.0
Transitive dependencies
sky_engine 0.0.99
sqflite_common 1.0.0+1
synchronized 2.2.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
build_runner ^1.7.3
collection ^1.14.11 1.14.11 1.14.12
matcher ^0.12.6
mockito ^4.1.1