Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.turso.tech/llms.txt

Use this file to discover all available pages before exploring further.

This SDK is community maintained and may not be officially supported by Turso, or up to date with the latest features. Join the #libsql-dart channel on Discord for help and feedback.
In this Flutter/Dart quickstart we will learn how to:
  • Retrieve database credentials
  • Install the Flutter/Dart libSQL client
  • Connect to a local or remote Turso database
  • Execute a query using SQL
  • Sync changes to local database (optional)
1

Retrieve database credentials

You will need an existing database to continue. If you don’t have one, create one.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=
You will want to store these as environment variables.
2

Install libsql_dart

flutter pub add libsql_dart
3

Initialize

Now connect to your local or remote database using the LibsqlClient constructor:
final dir = await getApplicationCacheDirectory();
final path = '${dir.path}/local.db';

final client = LibsqlClient(path)
  ..authToken = '<TOKEN>'
  ..syncUrl = '<TURSO_OR_LIBSQL_URL>'
  ..syncIntervalSeconds = 5
  ..readYourWrites = true;
final dir = await getApplicationCacheDirectory();
final path = '${dir.path}/local.db';

final client = LibsqlClient(path);
final client = LibsqlClient('<TURSO_OR_LIBSQL_URL>')
  ..authToken = '<TOKEN>';
4

Connect

await client.connect();
5

Execute

await client.execute("create table if not exists customers (id integer primary key, name text);");
If you need to use placeholders for values, you can do that:
final statement = await client
  .prepare("select * from customers where id = ?");

await statement.query(positional: [1])
6

Sync (Embedded Replicas only)

When using embedded replicas you should call sync() on the connector to sync your local database with the primary database.
await client.sync();