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

# Flutter Experiments

> A/B test with deterministic, offline-capable bucketing

> [!NOTE]
>
> **Supported on:** iOS SDK `1.0.61+` · Android SDK `1.0.33+` · Flutter SDK `1.0.3+`

The experiments module assigns users to variants using a deterministic hash. No server call is needed -- assignment is instant, works offline, and the same user always gets the same variant (as long as the user is in the experiment -- see [Who Is in an Experiment](https://docs.appdna.ai/guides/experiments.md#who-is-in-an-experiment)).

## Get Variant

```dart theme={null}
final variant = await AppDNA.experiments.getVariant("paywall-test");

switch (variant) {
  case "control":
    showStandardPaywall();
    break;
  case "variant_a":
    showNewPaywall();
    break;
  default:
    // null: not running (paused, completed, archived) or not fetched yet — always handle this branch.
    showStandardPaywall();
}
```

`getVariant` is asynchronous and returns the variant ID set in the Console or `null` if the experiment is not in the config the SDK holds, is not running (paused, completed or archived), does not target the current platform, or the user is outside its targeting rules or traffic allocation (SDK `1.0.20+`, evaluated by the native SDK; see [Who Is in an Experiment](https://docs.appdna.ai/guides/experiments.md#who-is-in-an-experiment)). `experimentId` is the experiment's ID, not its name -- copy it from the experiment's page or the **ID** column of the experiments list in the Console; `"paywall-test"` above is a placeholder.

> [!WARNING]
>
> Always handle the `null` / default case. Experiments can be archived or stopped at any time from the Console.

## Check Membership

When you only need a boolean for a specific variant, `isInVariant` is a convenient shortcut:

```dart theme={null}
if (await AppDNA.experiments.isInVariant("paywall-test", "variant_a")) {
  showNewPaywall();
} else {
  showStandardPaywall();
}
```

## Module Access

```dart theme={null}
final experiments = AppDNA.experiments;
```

### Module Methods

| Method | Signature | Description |
| - | - | - |
| `getVariant` | `Future<String?> getVariant(String experimentId)` | Get the assigned variant |
| `isInVariant` | `Future<bool> isInVariant(String experimentId, String variantId)` | Check if the user is in a specific variant |
| `getExposures` | `Future<List<Map<String, dynamic>>> getExposures()` | Read recorded experiment exposures for the current user |

## Exposure Tracking

The SDK automatically tracks an `experiment_exposure` event the first time it assigns a variant for a given experiment (on `getVariant()`, `isInVariant()` or a servable surface), and again only after an app restart or `reset()`. A user outside the experiment's targeting or traffic allocation gets `null` and records no exposure. Exposure events power the metrics that the experimentation engine uses to compute results -- you do not need to fire them yourself.

To inspect what the SDK has already recorded for the current user, call `getExposures()`:

```dart theme={null}
final exposures = await AppDNA.experiments.getExposures();

for (final exposure in exposures) {
  final experimentId = exposure["experimentId"] as String?;
  final variant = exposure["variant"] as String?;
  print("Exposed to $experimentId -> $variant");
}
```

This is useful for debugging, surfacing the active variant in a developer panel, or attaching the current bucket to your own analytics events.

## Reacting to Assignment Changes

The SDK does not push assignment-change callbacks. Variant assignments are stable for a given user ID and experiment: variants and weights cannot change while an experiment runs. An assignment changes when the user ID changes (`identify()` or `reset()`), and `getVariant` returns `null` once the experiment is paused, completed or archived and the SDK has fetched config. If you need to refresh UI when the experiment configuration could have changed, re-read `getVariant` at natural moments in your app:

* On app launch and on return from background
* When you present the screen that depends on the variant
* After a known config-refreshing action (e.g. login, environment switch)

```dart theme={null}
class HomeScreenController {
  String? _paywallVariant;

  Future<void> refresh() async {
    _paywallVariant = await AppDNA.experiments.getVariant("paywall_redesign");
    // re-render UI that depends on _paywallVariant
  }
}
```

> [!NOTE]
>
> Assignment changes are rare. They occur when the user ID changes or the experiment stops, not on every session start.

## Experiment Lifecycle

| Status | `getVariant()` returns |
| - | - |
| **Draft** | `null` -- not visible to SDKs |
| **Running** | Assigned variant string |
| **Paused** | `null` -- servable surfaces show the live entity |
| **Completed** | `null` -- removed from config. A servable surface's winner is live only if it was rolled out ([Auto-Deploy Winner](https://docs.appdna.ai/guides/experiments.md#auto-deploy-winner) or **Deploy winner**) |
| **Archived** | `null` -- removed from config |

A status change reaches the device with its next config fetch; until then the SDK keeps the definition it has. See [Experiment Lifecycle](https://docs.appdna.ai/guides/experiments.md#experiment-lifecycle).

## Full Example

```dart theme={null}
import 'package:appdna_sdk/appdna_sdk.dart';

class PaywallExperiment {
  Future<void> showPaywall() async {
    final variant =
        await AppDNA.experiments.getVariant("paywall_redesign");

    final paywallId =
        variant == "new_design" ? "paywall_v2" : "paywall_v1";

    await AppDNA.paywall.present(
      paywallId,
      context: PaywallContext(
        placement: "settings",
        customData: {
          "experiment": "paywall_redesign",
          "variant": variant ?? "control",
        },
      ),
    );
  }
}
```

> [!NOTE]
>
> Experiments are created in the Console under **Experiments**. The SDK uses MurmurHash3 for deterministic assignment -- the same user ID always gets the same variant across sessions, platforms, and offline.


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