---
title: "Flutter SDK"
source: https://docs.myappaffiliate.com/sdk-flutter
docs: "MyAppAffiliate — Developer Documentation"
index: https://docs.myappaffiliate.com/llms.txt
---
# Flutter SDK

One integration for iOS + Android, in Dart. Persists via `shared_preferences`, talks
plain HTTPS.

**Prompt for an AI coding agent**

```text
Integrate MyAppAffiliate into my project.

Documentation (read these first, they are the source of truth):
- Page: https://docs.myappaffiliate.com/sdk-flutter.md
- Full docs index: https://docs.myappaffiliate.com/llms.txt

Task: Add the MyAppAffiliate Flutter SDK to my app: add the pubspec dependency, start it once in main(), and identify the user with the same id my billing provider reports.

Rules:
- Follow the documented API exactly — no invented method names, endpoints, parameters or field names.
- Match the conventions already used in my codebase.
- Ask me for my SDK key instead of guessing, and never hard-code it — read it from config/environment.
- Do NOT set an API base URL anywhere. Every SDK compiles the production host in; a staging or self-hosted host belongs in a build setting, not in code.
- Tell me afterwards which steps I still have to do by hand (dashboard settings, capabilities, webhook configuration).
```

## Install (pubspec)

```yaml
dependencies:
  myappaffiliate_flutter: ^0.2.0
```

```bash
flutter pub get
```

## 1. Start it

```dart
import 'package:myappaffiliate_flutter/myappaffiliate_flutter.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await MyAppAffiliate.start(apiKey: 'pk_live_…');
  runApp(const MyApp());
}
```

You never pass an API URL — the host is compiled in.

### Keeping the key out of source

Supply it at build time and the call takes no arguments at all:

```bash
flutter build ios --dart-define=MYAPPAFFILIATE_API_KEY=pk_live_…
```

```dart
await MyAppAffiliate.start();
```

## 2. Identify the user

Call this once you know who the user is. The id must be the **same string your
billing provider reports back to us** — RevenueCat's `logIn(...)` id, Adapty's
customer user id, Superwall's app user id, or your Stripe/Paddle metadata value. See
[Billing & Webhooks](https://docs.myappaffiliate.com/webhooks).

```dart
await MyAppAffiliate.identify(userId);
```

That's the whole integration. There is no third step — in particular, you do **not**
need to set an `affiliate_id` subscriber attribute anywhere; attribution joins on the
user id.

## Optional

**Creator codes** — a "Got a code?" field. Works with no deep-link setup at all:

```dart
await MyAppAffiliate.applyCode('LUMI');
```

**Incoming links** — Flutter has no built-in link stream, so pass URIs through from
`app_links` (or `uni_links`) if links open your app directly:

```dart
final appLinks = AppLinks();
appLinks.uriLinkStream.listen(MyAppAffiliate.attribute);
```

**Read the attribution** for your own UI or analytics:

```dart
final affiliateId = await MyAppAffiliate.attributedAffiliateId();  // String?
```

**Staging or self-hosted API**, and debug logging:

```dart
await MyAppAffiliate.start(
  apiKey: 'pk_live_…',
  apiBaseUrl: 'https://staging.example.com',
  debug: true,
);
```

(Or build with `--dart-define=MYAPPAFFILIATE_API_BASE_URL=…` and pass nothing.)

**On logout or a data-erasure request:** `await MyAppAffiliate.reset();`

## What `start` does for you

- Generates and persists a device id via `shared_preferences`.
- Retries a referral an earlier launch captured but failed to send.
- On a fresh install with no link to go on, asks the API for a deferred match.

Every call completes with `false`/`null` on failure — the SDK never throws into your
app, and calls before `start` are safe no-ops.

## API

| Call | Purpose |
|---|---|
| `MyAppAffiliate.start(apiKey:)` | Start once at launch (key optional with `--dart-define`) |
| `MyAppAffiliate.identify(userId)` | Bind your user id to the attribution |
| `MyAppAffiliate.attribute(uri)` | Claim a referral from an incoming link |
| `MyAppAffiliate.applyCode(code)` | Creator-code entry |
| `MyAppAffiliate.attributedAffiliateId()` | The attributed affiliate id (or `null`) |
| `MyAppAffiliate.reset()` | Clear all persisted SDK state |

## Privacy

No advertising ID, no fingerprinting, no cross-app tracking. The SDK stores only a
generated device id and the attributed affiliate id. Attribution is first-party and
deterministic.
