drift 1.0.1
drift: ^1.0.1 copied to clipboard

Drift is a reactive library to store relational data in Dart and Flutter applications.

example/main.dart

// For more information on using drift, please see https://drift.simonbinder.eu/docs/getting-started/

import 'package:drift/drift.dart';
import 'package:drift/native.dart';

part 'main.g.dart';

class TodoItems extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get title => text()();
  TextColumn get content => text().nullable()();
}

@DriftDatabase(tables: [TodoItems])
class Database extends _$Database {
  Database(QueryExecutor e) : super(e);

  @override
  int get schemaVersion => 1;

  @override
  MigrationStrategy get migration {
    return MigrationStrategy(
      onCreate: (m) async {
        await m.createAll();

        // Add a bunch of default items in a batch
        await batch((b) {
          b.insertAll(todoItems, [
            TodoItemsCompanion.insert(title: 'A first entry'),
            TodoItemsCompanion.insert(
              title: 'Todo: Checkout drift',
              content: const Value('Drift is a persistence library for Dart '
                  'and Flutter applications.'),
            ),
          ]);
        });
      },
    );
  }

  // The TodoItem class has been generated by drift, based on the TodoItems
  // table description.
  //
  // In drift, queries can be watched by using .watch() in the end.
  // For more information on queries, see https://drift.simonbinder.eu/docs/getting-started/writing_queries/
  Stream<List<TodoItem>> get allItems => select(todoItems).watch();
}

Future<void> main() async {
  // Create an in-memory instance of the database with todo items.
  final db = Database(NativeDatabase.memory());

  db.allItems.listen((event) {
    print('Todo-item in database: $event');
  });

  // Add another entry
  await db
      .into(db.todoItems)
      .insert(TodoItemsCompanion.insert(title: 'Another entry added later'));

  // Delete all todo items
  await db.delete(db.todoItems).go();
}
31
likes
130
pub points
84%
popularity

Publisher

simonbinder.eu

Drift is a reactive library to store relational data in Dart and Flutter applications.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

async, collection, convert, meta, sqlite3, stream_channel

More

Packages that depend on drift