// ignore_for_file: directives_ordering // #docregion open // To open the database, add these imports to the existing file defining the // database class. They are used to open the database. import 'dart:io'; import 'package:drift/native.dart'; import 'package:path_provider/path_provider.dart'; import 'package:path/path.dart' as p; // #enddocregion open // #docregion overview import 'package:drift/drift.dart'; // assuming that your file is called filename.dart. This will give an error at // first, but it's needed for drift to know about the generated code part 'filename.g.dart'; // this will generate a table called "todos" for us. The rows of that table will // be represented by a class called "Todo". class Todos extends Table { IntColumn get id => integer().autoIncrement()(); TextColumn get title => text().withLength(min: 6, max: 32)(); TextColumn get content => text().named('body')(); IntColumn get category => integer().nullable()(); } // This will make drift generate a class called "Category" to represent a row in // this table. By default, "Categorie" would have been used because it only //strips away the trailing "s" in the table name. @DataClassName('Category') class Categories extends Table { IntColumn get id => integer().autoIncrement()(); TextColumn get description => text()(); } // this annotation tells drift to prepare a database class that uses both of the // tables we just defined. We'll see how to use that database class in a moment. // #docregion open @DriftDatabase(tables: [Todos, Categories]) class MyDatabase extends _$MyDatabase { // #enddocregion overview // we tell the database where to store the data with this constructor MyDatabase() : super(_openConnection()); // you should bump this number whenever you change or add a table definition. // Migrations are covered later in the documentation. @override int get schemaVersion => 1; // #docregion overview } // #enddocregion overview LazyDatabase _openConnection() { // the LazyDatabase util lets us find the right location for the file async. return LazyDatabase(() async { // put the database file, called db.sqlite here, into the documents folder // for your app. final dbFolder = await getApplicationDocumentsDirectory(); final file = File(p.join(dbFolder.path, 'db.sqlite')); return NativeDatabase(file); }); } // #enddocregion open // #docregion usage Future main() async { final database = MyDatabase(); // Simple insert: await database .into(database.categories) .insert(CategoriesCompanion.insert(description: 'my first category')); // Simple select: final allCategories = await database.select(database.categories).get(); print('Categories in database: $allCategories'); } // #enddocregion usage