> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trevosdk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Flutter

> Experiments and tracking for Flutter apps on iOS and Android, assigning the same variants as the web.

`trevosdk_flutter` runs experiments and records events in Flutter apps, assigning the same
variant the web SDK does for the same user. Written in Dart; its one dependency is
`path_provider`. Flutter 3.24 and later, on iOS and Android.

```bash theme={null}
flutter pub add trevosdk_flutter
```

## Create the client

Use a **Mobile** key (`tsk_live_…`), passed in at build time with `--dart-define`. Pass the
installed build's version too; [package\_info\_plus](https://pub.dev/packages/package_info_plus)
reads it. Create one client for the app's lifetime:

```dart theme={null}
import 'package:flutter/widgets.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:trevosdk_flutter/trevosdk_flutter.dart';

late final TrevoClient trevo;

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final info = await PackageInfo.fromPlatform();
  trevo = TrevoClient(TrevoOptions(
    apiKey: const String.fromEnvironment('TREVO_API_KEY'),
    appVersion: info.version,
  ));
  runApp(const App());
}
```

`appVersion` is required on purpose: without the real build version, a `minAppVersion`
experiment returns control for everyone. Never hard-code a sample version.

## Read a variant

`getVariant` is synchronous. It returns `control` until stored state and the first config
fetch have loaded, so wait for `ready()` before showing experiment UI:

```dart theme={null}
FutureBuilder<void>(
  future: trevo.ready(),
  builder: (context, snapshot) {
    if (snapshot.connectionState != ConnectionState.done) return const SizedBox.shrink();
    final variant = trevo.getVariant('checkout-v2');
    return ElevatedButton(
      onPressed: () => trevo.track('checkout_started'),
      child: Text(variant == 'treatment' ? 'Start free trial' : 'Subscribe'),
    );
  },
)
```

Each call records an **exposure** the first time. Pass `trackExposure: false` to read without
recording one. `onChange` calls a listener whenever config or identity changes and returns a
function that stops it.

## Parameters

A [parameter](https://docs.trevosdk.com/concepts/parameters.md) lets later experiments change a value with no release:

```dart theme={null}
final headline = trevo.getParameter('paywall.headline', 'Subscribe');
final trialDays = trevo.getParameter('paywall.trial_days', 7);
```

The default's type decides the result's: `String`, `bool`, `int` or `double`. A value of another
type, or a number that isn't whole for an `int`, serves the default.

A `String` parameter can carry [text per language](https://docs.trevosdk.com/concepts/parameters.md#lists-and-translated-text):
pass your app's own translation as the default and call `trevo.setLocale(...)` with the locale your
app shows, such as `Localizations.localeOf(context).toLanguageTag()`, since only your app knows it.
Without it the SDK reads the device's language.

## Track events and identify users

```dart theme={null}
trevo.track('checkout_completed', properties: {'plan': 'pro', 'seats': 3});

trevo.identify(user.id);   // after sign-in, and on launch when auth restores a user
await trevo.reset();       // on sign-out: clears the user and starts a new anonymous id
```

Events are queued on disk and sent in batches every 15 seconds, when the app moves to the
background, and on `await trevo.flush()`.

The SDK keeps its files in `trevosdk` under the app support directory, which device backups
include. A device restored from a backup then shares the anonymous id of the one it came from.
On Android you can leave the folder out of backups in your `data_extraction_rules.xml` (and
`full_backup_content` for Android 11 and older):

````xml theme={null}
<exclude domain="file" path="trevosdk/" />
``` See [Mobile experiments](https://docs.trevosdk.com/concepts/mobile-experiments.md)
for how experiments ramp on mobile, how the SDK survives a lost network, and the App Store and
Google Play answers.

## Options

| Option | Default | Notes |
| --- | --- | --- |
| `apiKey` | | Required. A Mobile key, `tsk_live_…` |
| `appVersion` | | Required, may be null. Sent on every event; compared with `minAppVersion` |
| `bootstrapConfig` | `[]` | Assign before storage and the network answer |
| `pollInterval` | 5 minutes | Config is also refreshed on every foreground |
| `flushInterval` | 15 seconds | Also flushes when the app moves to the background |
| `maxBatchSize` | 50 | Capped at 500 by the server |
| `configUrl`, `ingestionUrl` | Trevo | Endpoint overrides for proxied installs |
| `storage`, `transport`, `lifecycle` | Files, `dart:io`, Flutter lifecycle | Replace for tests or your own stack |
| `onError` | `debugPrint` | Background failures; the SDK never throws |

## Correctness

The package runs the shared conformance vectors in CI, so a change that would assign a user
differently from the web fails the build. The rules are in the
[bucketing spec](https://docs.trevosdk.com/reference/bucketing-spec.md).
````


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.