Turso Cloud SDKs Community SDKs Flutter / Dart Integrations
Drift + Turso / LibSQL
Configure `drift` to work with `libsql_dart`
Prerequisites
Before you start, make sure you:
1. Add packages to your project
flutter pub add drift_libsql drift drift_flutter dev:build_runner dev:drift_dev
2. Retrieve database credentials
Get the database URL:
turso db show --url <database-name>
Get the database authentication token:
turso db tokens create <database-name>
Assign credentials to the environment variables inside .env.
TURSO_DATABASE_URL=
TURSO_AUTH_TOKEN=
3. Create a table schema
class TaskTable extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get title => text()();
TextColumn get description => text()();
BoolColumn get completed => boolean()();
}
4. Create db class
@DriftDatabase(tables: [TaskTable])
class AppDatabase extends _$AppDatabase {
AppDatabase(super.e);
@override
int get schemaVersion => 1;
}
5. Run build runner
dart run build_runner build
6. Create the db
final db = AppDatabase(DriftLibsqlDatabase(
"${dir.path}/replica.db",
syncUrl: url,
authToken: token,
readYourWrites: true,
syncIntervalSeconds: 3,
));
7. Perform SQL operations
await db.into(db.taskTable).insert(TaskTableCompanion.insert(
title: task.title,
description: task.description,
completed: task.completed)); Synced from official Turso documentation (
tursodatabase/turso-docs
· MIT License)
Official Docs ↗