# Flutter reference

> Every public type in the FacePing Flutter package: setup, enrolment, verification, the camera widget and results.

Source: https://docs.faceping.ai/sdk/flutter/

Package: [`faceping`](https://pub.dev/packages/faceping) (pub.dev, preview). Starter app: [faceping/flutter-starter](https://github.com/faceping/flutter-starter). Flutter 3.24 or later (Dart 3.5). Android 7 (API 24) or later, on 64-bit phones (`arm64-v8a`) and `x86_64` emulators. iOS 15.1 or later; Xcode 26 or later.

```bash
flutter pub add faceping:1.0.0-preview.6
```

The package is a thin bridge over the native [Android](https://docs.faceping.ai/sdk/android/) and [iOS](https://docs.faceping.ai/sdk/ios/) SDKs, so results, errors and timings are the same as in a native app. Everything is in `package:faceping/faceping.dart`.

## Platform setup

**iOS.** Set the minimum iOS version to **15.1**: in Xcode, open `ios/Runner.xcworkspace` and set the Runner project's **Minimum Deployments** to 15.1 (with CocoaPods, also `platform :ios, '15.1'` in `ios/Podfile`). Add a camera usage description to `ios/Runner/Info.plist`; without it, iOS stops the app when the camera starts:

```xml title="ios/Runner/Info.plist"
<key>NSCameraUsageDescription</key>
<string>We use the front camera to check it's you.</string>
```

Flutter fetches the FacePing Swift package ([faceping/faceping-ios](https://github.com/faceping/faceping-ios)) with Swift Package Manager, which current Flutter uses by default. If your app has Swift Package Manager turned off, the podspec downloads the same release (`FacePing.xcframework.zip`) during `pod install` and checks its SHA-256 before using it. The simulator has no front camera: test the live check on an iPhone.

**Android.** No permissions to add: the SDK (`ai.faceping:faceping-android`, from Maven Central) declares `CAMERA` and `INTERNET`. Do keep FacePing's data out of backups (see [Backups](#backups)). To keep your app small, build for the two ABIs FacePing supports:

```bash
flutter build apk --release --split-per-abi --target-platform android-arm64,android-x64   # one APK per ABI
flutter build appbundle --release                                                         # for Google Play
```

Don't add `ndk { abiFilters … }` to `android/app/build.gradle.kts` for this: it conflicts with `--split-per-abi`, and the build stops.

On a 32-bit Android phone the app still runs, and FacePing throws `FacePingModelException` so you can send people to your normal way in.

## Setup

```dart
Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // Returns at once and loads the face models in the background.
  await FacePing.init(sandboxKey: 'fp_test_…');   // sandbox: faces enrolled on the device
  // await FacePing.init(deviceToken: 'fpd_…');   // production or your practice list: faces synced from that list
  runApp(const MyApp());
}
```

`FacePing.current` returns the same instance anywhere in your app, and throws `StateError` until `init` has completed (`FacePing.currentOrNull` returns null instead). Calling `init` again with the same options returns the same instance; with different options it throws `FacePingInvalidOperationException` (call `close()` first).

A key that doesn't start with `fp_test_` throws `FacePingSetupException` from `init`. A key the server doesn't accept, or a first run with no connection, is reported by the first call that needs the network, so call `sync()` at start-up to find out early.

### FacePingOptions

For every option, call `FacePing.initWithOptions(FacePingOptions(...))`.

| Property | Type | Default | Notes |
|---|---|---|---|
| `sandboxKey` | `String?` | `null` | A `fp_test_` key from [**Developers → Sandbox keys**](https://app.faceping.ai/developers). |
| `deviceToken` | `String?` | `null` | Production, or your sandbox's practice list. A `fpd_` device token for one check-in list (from `POST /v1/lists/{listId}/devices`, or the list's Devices tab). Set this or `sandboxKey`, not both. |
| `region` | `FacePingRegion` | `eu` | Your account's region, which sets the API address. Today the only value is EU (`https://api-eu.faceping.ai/`). |
| `apiBaseUrl` | `String?` | `null` | Overrides the region's address. UK and US regions are available on request: we give you the address to put here. |
| `passiveLiveness` | `bool` | `true` | Passive liveness on every check. Turn it off only if your app runs its own liveness check first. |
| `autoSyncInterval` | `Duration?` | 5 minutes | With a device token (production, or a practice-list device). `null` turns automatic sync off; call `sync()` yourself. |

## FacePing

```dart
class FacePing {
  static Future<FacePing> init({String? sandboxKey, String? deviceToken, FacePingRegion region});
  static Future<FacePing> initWithOptions(FacePingOptions options);
  static FacePing get current;
  static FacePing? get currentOrNull;
  static Future<String> get sdkVersion;     // the native SDK's version

  FacePingOptions get options;
  bool get isSandbox;                   // true with a sandbox key; false with a device token, even for a practice list

  Future<EnrollResult> enroll(String id, FaceImage photo);
  Future<VerifyResult> verifyLive(String id, FacePingCameraController camera, {String? nonce});
  Future<VerifyResult> verify(String id, FaceImage photo, {String? nonce});

  Future<bool> forget(String id);
  Future<void> forgetAll();
  Future<void> unpair();
  Future<Set<String>> enrolledIds();

  Future<SyncResult> sync();
  Stream<SyncResult> get synced;            // every sync, automatic or not
  Future<void> close();
}
```

| Method | Notes |
|---|---|
| `enroll` | Sandbox only. Production enrolment happens with consent on your server or through the FacePing widget. Replaces any face already stored under `id`. |
| `verifyLive` | A live check on a `FaceCamera`: match, a head turn in a random direction, match again. Shows the prompts and sets the guide. About 10 seconds at most; it then returns `noFace` or the last failure. Stop it early with `camera.cancel()`. |
| `verify` | One photo, passive liveness only. |
| `nonce` | Check-in devices: a nonce from your server, signed into the result's `signedResult` so your server can tell the result answers that check. 1 to 128 characters, no control characters (anything else throws `FacePingInvalidArgumentException`). See [Signed check results](https://docs.faceping.ai/signed-results/). |
| `forget` | With a sandbox key: deletes the face stored under `id` from the device now, and returns `false` if there was none. With a device token (production, or a practice-list device): throws `FacePingInvalidOperationException`; remove a person through your server instead. |
| `forgetAll` | Deletes every face on the device now. In production the next sync brings back the people still enrolled. |
| `unpair` | Unpairs the device: deletes every face on it and forgets the check-in list it synced, so it can start again with any device token. Moving to another list doesn't need this: start with that list's token and the first successful sync swaps the lists, wiping the old faces first ([Move a device to another list](https://docs.faceping.ai/devices/#move-a-device-to-another-list-or-retire-it)). Use it when a device is retired or handed back, and clear your saved token too. In the sandbox it deletes the faces enrolled on the device. Then `close()` and `FacePing.init` with the new token. |
| `enrolledIds` | Who can be verified on this device, as of the last enrol, forget or sync. |
| `sync` | Production: downloads the latest faces for the device's check-in list; a revoked device wipes its faces. Sandbox: activates the key, or renews its licence when due. Either way it refreshes `enrolledIds`, so it's a good first call when the app starts. |
| `close` | Stops automatic sync and frees the engine. `FacePing.current` is cleared; call `init` again to start over. |

## FaceImage

What you can pass as a photo.

| From | Example |
|---|---|
| The camera | `await camera.capture()` |
| JPEG or PNG bytes (HEIC too on iOS) | `FaceImage.fromBytes(bytes)` |
| A photo file on the device, for example from an image picker | `FaceImage.fromFile(path)` |

Photos are turned upright from their EXIF orientation and scaled down to 1280 pixels on the device before anything else happens. Bytes that aren't a readable image throw `FacePingInvalidArgumentException` when the photo is used.

## FaceCamera

The native front camera, with an oval face guide and the liveness prompts ("Turn your head left", "Look back at the camera"). The SDK runs the camera, so you don't need another camera package, and asks for camera permission the first time the widget appears. Give it a fixed height.

```dart
final camera = FacePingCameraController();

SizedBox(
  height: 420,
  child: FaceCamera(
    controller: camera,
    onCameraFailed: (message) => showMessage(message), // permission refused, or no front camera
  ),
)
```

| `FaceCamera` | Notes |
|---|---|
| `controller` | A `FacePingCameraController`. Create it once (for example in your `State`) and give it to one `FaceCamera` at a time. |
| `onCameraFailed` | Called when the camera can't start: permission refused or no front camera. The message says which. |
| `androidHybridComposition` | Android: Hybrid Composition, the default and the most faithful for camera previews. Set `false` for texture layer composition. |

| `FacePingCameraController` | Notes |
|---|---|
| `capture()` | Takes a still from the live view: upright, as the camera sees it (the preview is mirrored, the picture isn't). Throws `FacePingCameraException` if the camera doesn't deliver a picture within 5 seconds. |
| `setGuideState(state)` | How the guide looks: `ready` (dashed), `working` (a scan line), `match` (green) or `noMatch` (red). `verifyLive` sets it; set it yourself for other steps, for example `working` while enrolling. |
| `setShowGuide(show)` | Shows or hides the face outline. |
| `startCamera()` | Starts (or restarts) the camera, for example after your app got camera permission another way. |
| `cancel()` | Stops a live check on this camera; its `verifyLive` throws `FacePingCanceledException`. |
| `isAttached` | True while its `FaceCamera` is on screen. |

To translate or reword the prompts, use the native SDKs' strings: on Android the string resources `faceping_prompt_look_at_camera`, `faceping_prompt_turn_left`, `faceping_prompt_turn_right` and `faceping_prompt_look_back`; on iOS a `FacePing.strings` table with the keys `faceping.prompt.lookAtCamera`, `faceping.prompt.turnLeft`, `faceping.prompt.turnRight` and `faceping.prompt.lookBack`.

## Backups

The face store's encryption keys stay in the phone's Keychain or Keystore. On iOS, FacePing's folder is already excluded from backups. On Android, set `android:allowBackup="false"`, and from Android 12 exclude `faceping/` (in the app's files) and the shared preferences `faceping.keys.xml` from `cloud-backup` and `device-transfer` in your data extraction rules: [Allow the camera](https://docs.faceping.ai/start/flutter/#allow-the-camera) in the guide has the file.

## Results

```dart
class EnrollResult {
  final EnrollOutcome outcome;
  final String id;
  bool get isEnrolled;          // outcome == EnrollOutcome.enrolled
}

enum EnrollOutcome { enrolled, noFace, multipleFaces, faceTooSmall, notLive, limitReached }

class VerifyResult {
  final VerifyOutcome outcome;
  final double similarity;      // cosine similarity; the match threshold is 0.363
  final double? livenessScore;  // passive liveness of the deciding frame
  final Duration elapsed;       // how long the check took on the device
  final bool isSandbox;
  final String? signedResult;   // check-in devices: the signed result for your server; null in the sandbox
  bool get isMatch;             // outcome == VerifyOutcome.match
}

enum VerifyOutcome {
  match, noMatch, notEnrolled, noFace, livenessFailed, challengeFailed, expired, leaseExpired
}

class SyncResult {
  final SyncStatus status;      // ok, gone, revoked or unavailable
  final int enrolled;
  final DateTime? leaseUntil;   // production: verifies offline until then
  final DateTime? deleteAfter;
  final SigningState signing;   // registered, mismatch or unknown
}
```

What a sync status means, with a device token:

| Status | What happened | What the app should do |
|---|---|---|
| `ok` | The device has the list's latest faces. | Nothing: it verifies offline until the lease runs out. |
| `gone` | The list is past its deletion date, so the device wiped its copy. | Stop offering face check-in for this list. |
| `revoked` | The device token was revoked, isn't valid, or its list was deleted, so the device wiped its copy. | Stop offering face check-in, and pair the device again with a new token if it should carry on. |
| `unavailable` | No connection, or the server couldn't be reached. The device keeps its copy. | Carry on: it verifies offline until the lease runs out, then needs a sync. |

`signedResult` is set on a check-in device (a device token): every outcome is signed by the device, so your server can prove the check happened. It's `null` in the sandbox, and until the device's first sync. `signing` says whether FacePing holds the device's signing key: `registered`; `mismatch` (the app was reinstalled or its data cleared, so checks still work but signed results won't verify: add the device again); or `unknown` (before the first sync, and in the sandbox). See [Signed check results](https://docs.faceping.ai/signed-results/).

`limitReached` means the sandbox already holds 25 faces on this device: forget one first. `leaseExpired` (production) means the device hasn't synced for longer than its lease, 24 hours by default; it needs a connection before it verifies again.

## Exceptions

Everything FacePing throws is a `FacePingException` with a `message` ready to show.

| Exception | When |
|---|---|
| `FacePingSetupException` | The key or token is missing or malformed (neither set, both set, or a sandbox key that doesn't start with `fp_test_`), or the sandbox key couldn't be activated (the first run needs a connection, or the server refused the key). A revoked or mistyped device token throws this at a device's first sync; once a device has synced, a revoked token or a deleted list doesn't throw: sync returns `revoked` and the device wipes its copy. |
| `FacePingModelException` | The face models couldn't load on this device. Send people to your normal way in. |
| `FacePingInvalidOperationException` | Not allowed in this mode or state, for example enrolling in production or a closed FacePing. |
| `FacePingInvalidArgumentException` | A bad argument: an empty id, a photo that isn't a readable image, or a nonce that isn't 1 to 128 characters. |
| `FacePingStorageException` | The device's secure storage failed. |
| `FacePingCameraException` | The camera isn't on screen, couldn't start or didn't deliver a picture. |
| `FacePingCanceledException` | A live check was stopped with `camera.cancel()`. |

Everything else is a result, not an exception: a face check never throws because someone looked away.

## Testing

`package:faceping/faceping_platform_interface.dart` exports `FacePingPlatform`. Set `FacePingPlatform.instance` to a fake in your unit tests to run your app's logic without a device.
