# Turso Quickstart (Flutter / Dart)

> Get started with Flutter and Dart using the libSQL client in a few simple steps
> Canonical HTML: https://tursodb.cn/docs/sdk/flutter/quickstart/
> Upstream Source (MIT): https://docs.turso.tech/sdk/flutter/quickstart
> Language: en

---
<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

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](https://tur.so/discord) for help and feedback.

</div></div>


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](/docs/quickstart/).

Get the database URL:

```bash
turso db show --url <database-name>
```

Get the database authentication token:

```bash
turso db tokens create <database-name>
```

Assign credentials to the environment variables inside `.env`.

```bash
TURSO_DATABASE_URL=
TURSO_AUTH_TOKEN=
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

You will want to store these as environment variables.

</div></div>


**2. Install libsql\_dart**

```bash
flutter pub add libsql_dart
```

**3. Initialize**

Now connect to your local or remote database using the `LibsqlClient` constructor:

<details class="doc-accordion"><summary>Embedded Replicas</summary><div class="doc-accordion-body">

```dart
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;
```

</div></details>


<details class="doc-accordion"><summary>Local only</summary><div class="doc-accordion-body">

```dart File
final dir = await getApplicationCacheDirectory();
final path = '${dir.path}/local.db';

final client = LibsqlClient(path);
```

```dart In-Memory
final client = LibsqlClient(":memory:");
```

</div></details>


<details class="doc-accordion"><summary>Remote only</summary><div class="doc-accordion-body">

```dart
final client = LibsqlClient('<TURSO_OR_LIBSQL_URL>')
  ..authToken = '<TOKEN>';
```

</div></details>


**4. Connect**

```dart
await client.connect();
```

**5. Execute**

```dart Execute
await client.execute("create table if not exists customers (id integer primary key, name text);");
```

```dart Insert
await client.query("insert into customers(name) values ('John Doe')");
```

```dart Select
print(await client.query("select * from customers"));
```

```dart Batch
await client.batch("""insert into customers (name) values ('Jane Doe'); insert into customers (name) values ('Jake Doe');""");
```

```dart Prepared statement
final statement = await client
	.prepare("select * from customers where id = ?");

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

```dart Interactive transaction
final tx = await client.transaction();

await tx
	.execute("update customers set name = 'John Noe' where id = 1");
await tx
	.execute("update customers set name = 'Jane Noe' where id = 2");
print(await tx
	.query("select * from customers where id = ?", positional: [1]));

await tx.commit();
```

If you need to use placeholders for values, you can do that:

```dart Positional
final statement = await client
  .prepare("select * from customers where id = ?");

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

```dart Named
final statement = await client
	.prepare("select * from customers where id = :id");

await statement.query(named: {"id": 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.

```dart
await client.sync();
```
