Sub topic 14
Storing Custom Types with Converters
This chapter assumes you know basic Dart classes, enums, async code, and Drift table definitions. You should also understand that SQLite stores a small set of primitive types: integers, real numbers, text, and binary data. A custom type is a Dart type that does not directly match one of those SQLite types.
A type converter translates between the Dart value used by your app and the SQLite value stored on disk.
Why use a converter?
Suppose a task has a priority. In Dart, an enum communicates the allowed values clearly:
enum Priority { low, normal, high }SQLite cannot store a Dart enum directly. Store the enum name as text, then convert that text back into a Priority when Drift reads a row.
| Dart type | SQLite type | Typical representation |
|---|---|---|
| `Priority` | `TEXT` | `"high"` |
| `DateTime` | `INTEGER` or `TEXT` | Timestamp or ISO text |
| `Set<String>` | `TEXT` | JSON string |
A converter keeps this translation in one place. Queries and generated row classes can then use Priority instead of raw strings.
Writing an enum converter
A converter extends TypeConverter<DartType, SqlType>. The first type is the type used by Dart. The second type is the type stored by SQLite.
import 'package:drift/drift.dart';
enum Priority { low, normal, high }
class PriorityConverter extends TypeConverter<Priority, String> {
const PriorityConverter();
@override
Priority fromSql(String fromDb) {
return Priority.values.byName(fromDb);
}
@override
String toSql(Priority value) {
return value.name;
}
}fromSql runs when Drift reads text from SQLite. toSql runs before Drift writes a Priority value.
Apply the converter to a column with map:
class TodoItems extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get title => text()();
TextColumn get priority =>
text().map(const PriorityConverter()).withDefault(
const Constant('normal'),
)();
}The generated TodoItem row now exposes priority as a Priority, while the database stores values such as low and high.
The default value deserves attention. Constant('normal') is a SQLite text value, so it uses the storage representation rather than Priority.normal. For inserts from Dart, Drift applies the converter automatically.
Handling invalid stored values
Priority.values.byName throws if the database contains an unknown name. That may happen after a bad manual edit or an enum rename. Decide how your app should respond before shipping a migration.
For values that need stable names, define the mapping explicitly:
class PriorityConverter extends TypeConverter<Priority, String> {
const PriorityConverter();
@override
Priority fromSql(String value) => switch (value) {
'low' => Priority.low,
'normal' => Priority.normal,
'high' => Priority.high,
_ => throw FormatException('Unknown priority: $value'),
};
@override
String toSql(Priority value) => switch (value) {
Priority.low => 'low',
Priority.normal => 'normal',
Priority.high => 'high',
};
}This makes the on-disk format independent of Dart enum names.
The conversion path
Exercise
Exercise
Why should a converter use stable serialized names instead of relying on enum names when the database will live across app upgrades?
Show answer
Enum names can change during refactoring, but stored database values remain. Stable names preserve compatibility with existing rows.