moor 1.6.0

moor #

Moor is an easy to use, reactive persistence library for Flutter apps. Define your database tables in pure Dart and enjoy a fluent query API, auto-updating streams and more!

With Flutter #

This library defines the APIs for the moor persistence library. To use it on Flutter apps, you'll probably want to use the moor_flutter implementation directly.

For the web #

For information to use this library on the web (including Flutter web), follow the instructions here. Keep in mind that web support is still experimental.

Please see the homepage of moor or the moor_flutter package for details on how to use this package.

1.6.0 #

  • Experimental web support! See the documentation for details.
  • Make transactions easier to use: Thanks to some Dart async magic, you no longer need to run queries on the transaction explicitly. This
    Future deleteCategory(Category category) {
      return transaction((t) async {
        await t.delete(categories).delete(category);
    is now the same as this (notice how we don't have to use the t. in front of the delete)
      Future deleteCategory(Category category) {
        return transaction((t) async {
          await delete(categories).delete(category);
    This makes it much easier to compose operations by extracting them into methods, as you don't have to worry about not using the t parameter.
  • Moor now provides syntax sugar for list parameters in compiled custom queries (SELECT * FROM entries WHERE id IN ?)
  • Support COLLATE expressions.
  • Date time columns are now comparable
  • The StringType now supports arbitrary data from sqlite (#70). Thanks, knaeckeKami!
  • Bugfixes related to stream queries and LIMIT clauses.

1.5.1 #

  • Fixed an issue where transformed streams would not always update
  • Emit a INSERT INTO table DEFAULT VALUES when appropriate. Moor used to generate invalid sql before.

1.5.0 #

This version introduces some new concepts and features, which are explained in more detail below. Here is a quick overview of the new features:

  • More consistent and reliable callbacks for migrations. You can now use MigrationStrategy.beforeOpen to run queries after migrations, but before fully opening the database. This is useful to initialize data.
  • Greatly expanded documentation, introduced additional checks to provide more helpful error messages
  • New getSingle and watchSingle methods on queries: Queries that you know will only return one row can now be instructed to return the value directly instead of wrapping it in a list.
  • New "update companion" classes to clearly separate between absent values and explicitly setting values back to null - explained below.
  • Experimental support for compiled sql queries: Moor can now generate typesafe APIs for written sql. Read on to get started.

Update companions #

Newly introduced "Update companions" allow you to insert or update data more precisely than before. Previously, there was no clear separation between "null" and absent values. For instance, let's say we had a table "users" that stores an id, a name, and an age. Now, let's say we wanted to set the age of a user to null without changing its name. Would we use User(age: null)? Here, the name column would implicitly be set to null, so we can't cleanly separate that. However, with UsersCompanion(age: Value(null)), we know the difference between Value(null) and the default Value.absent().

Don't worry, all your existing code will continue to work, this change is fully backwards compatible. You might get analyzer warnings about missing required fields. The migration to update companions will fix that. Replacing normal classes with their update companions is simple and the only thing needed to fix that. The documentation has been updated to reflect this. If you have additional questions, feel free to create an issue.

Compiled sql queries #

Experimental support for compile time custom statements. Sounds super boring, but it actually gives you a fluent way to write queries in pure sql. The moor generator will figure out what your queries return and automatically generate the boring mapping part. Head on to the documentation to find out how to use this new feature.

Please note that this feature is in an experimental state: Expect minor, but breaking changes in the API and in the generated code. Also, if you run into any issues with this feature, reporting them would be super appreciated.

1.4.0 #

  • Added the RealColumn, which stores floating point values
  • Better configuration for the serializer with the JsonKey annotation and the ability to use a custom ValueSerializer

1.3.0 #

  • Moor now supports table joins
    • Added table aliases
  • Default values for columns: Just use the withDefault method when declaring a column
    • added expressions that resolve to the current date or time
  • Fixed a crash that would occur if the first operation was a transaction
  • Better support for custom expressions as part of a regular query
  • Faster hashcode implementation in generated data classes

1.2.0 #

  • Breaking: Generated DAO classes are now called _$YourNameHere, it used to be just _YourNameHere (without the dollar sign)
  • Blob data type
  • insertOrReplace method for insert statements
  • DAOs can now operate on transactions
  • Custom constraints
  • Query streams are now cached so that equal queries yield identical streams. This can improve performance.

1.1.0 #

  • Transactions

1.0.0 #

  • Initial version of the Moor library


import 'package:moor/moor.dart';

part 'example.g.dart';

// Define tables that can model a database of recipes.

class Categories extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get description => text().nullable()();

class Recipes extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get title => text().withLength(max: 16)();
  TextColumn get instructions => text()();
  IntColumn get category => integer().nullable()();

class Ingredients extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get name => text()();
  IntColumn get caloriesPer100g => integer().named('calories')();

class IngredientInRecipes extends Table {
  String get tableName => 'recipe_ingredients';

  // We can also specify custom primary keys
  Set<Column> get primaryKey => {recipe, ingredient};

  IntColumn get recipe => integer()();
  IntColumn get ingredient => integer()();

  IntColumn get amountInGrams => integer().named('amount')();

@UseMoor(tables: [Categories, Recipes, Ingredients, IngredientInRecipes])
class Database extends _$Database {
  Database(QueryExecutor e) : super(e);

  int get schemaVersion => 1;

  MigrationStrategy get migration {
    return MigrationStrategy(
      beforeOpen: (engine, details) async {
        // populate data
        await engine
            .insert(const CategoriesCompanion(description: Value('Sweets')));

Use this package as a library

1. Depend on it

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

  moor: ^1.6.0

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or 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:moor/moor.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 Aug 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:moor/moor.dart.

Health suggestions

Fix lib/src/runtime/database.dart. (-1 points)

Analysis of lib/src/runtime/database.dart reported 2 hints:

line 314 col 19: 'onFinished' is deprecated and shouldn't be used.

line 315 col 23: 'onFinished' is deprecated and shouldn't be used.

Fix lib/src/runtime/migration.dart. (-0.50 points)

Analysis of lib/src/runtime/migration.dart reported 1 hint:

line 55 col 14: 'onFinished' is deprecated and shouldn't be used.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
collection >=1.0.0 <2.0.0 1.14.12
meta >=1.0.0 <2.0.0 1.1.7
synchronized ^2.1.0 2.1.0+1
Dev dependencies
build_runner >=1.3.0 <2.0.0
build_test ^0.10.8
coverage ^0.12.4
grinder ^0.8.3
mockito ^4.1.0
moor_generator ^1.6.0
test ^1.6.4
test_api >=0.2.0 <1.0.0
test_core >=0.2.0 <1.0.0