drift 1.0.0 copy "drift: ^1.0.0" to clipboard
drift: ^1.0.0 copied to clipboard

outdated

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();
}
1500
likes
0
pub points
99%
popularity

Publisher

verified publishersimonbinder.eu

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

Homepage
Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

async, collection, convert, meta, sqlite3, stream_channel

More

Packages that depend on drift