Sub topic 3
Describing Your Schema as Dart Classes
This chapter assumes you know basic Dart and Flutter, can use async and await, and understand that a relational database stores data in tables and rows. You should also know how to run Flutter and Dart commands.
A database schema describes the shape of stored data. In drift, you describe that schema with Dart classes. Each class that extends Table becomes a SQLite table. Each getter inside the class becomes a column.
Define a table
The following table stores to-do items:
import 'package:drift/drift.dart';
class TodoItems extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get title =>
text().withLength(min: 1, max: 100)();
BoolColumn get isDone =>
boolean().withDefault(const Constant(false))();
DateTimeColumn get createdAt =>
dateTime().withDefault(currentDateAndTime)();
}The id column is an integer primary key. autoIncrement() tells SQLite to assign a new value when you insert a row.
The title column stores text. Its length constraint rejects empty values and values longer than 100 characters. The isDone column stores a Boolean value and defaults to false. The createdAt column stores a date and time and receives the current time by default.
The repeated final () calls are part of drift's column declaration syntax. The first call configures the column expression. The second call completes the getter declaration.
Choose column types and constraints
Drift provides Dart column types that map to SQLite storage types. Common choices include:
| Drift column | Typical Dart value | Common use |
|---|---|---|
| `integer()` | `int` | IDs, counts, flags |
| `text()` | `String` | Names, descriptions |
| `boolean()` | `bool` | On/off values |
| `dateTime()` | `DateTime` | Timestamps |
| `real()` | `double` | Measurements and prices |
Columns are required by default. Add nullable() when a value may be absent:
TextColumn get note => text().nullable()();A nullable column produces a String? value in the generated Dart row class. Use nullability only when “no value” has a clear meaning. For example, an optional note can be nullable, but a task title should remain required.
You can also add uniqueness and defaults:
TextColumn get externalId =>
text().unique()();
IntColumn get priority =>
integer().withDefault(const Constant(0))();A unique column prevents duplicate values. A default lets inserts omit a value and use the database-defined value instead.
Understand the generated model
After drift generates supporting code, TodoItems produces a row class named TodoItem. A row represents data read from the database. Its fields are ordinary Dart values such as id, title, and isDone.
Drift also generates a companion class named TodoItemsCompanion. A companion represents values being written. It can distinguish “set this column” from “leave this column at its database default,” which is important for auto-incremented and defaulted columns.
Exercise
Which declaration should you use for an optional text field that may be absent?
Show answer
TextColumn get note => text().nullable()();