# Flutter (Dart)

> Add ScreenlyyID identity verification to a Flutter app for iOS and Android: a back end that calls the API, and an app that opens the hosted page in a WebView.

Canonical URL: https://www.screenlyyid.com/docs/recipes/flutter/

The [ScreenlyyID Flutter example](https://github.com/ScreenlyyID/flutter-sdk-example) is the reference for a Flutter app that runs on iOS and Android. It mirrors the [iOS](https://www.screenlyyid.com/docs/recipes/ios/) and [Android](https://www.screenlyyid.com/docs/recipes/android/) examples and has the same two parts, and the split matters:

- **`backend/`**: a small Node server that holds the API key and the `CorrelationId` and makes every ScreenlyyID call. You can swap it for your own server, for instance the [Express](https://www.screenlyyid.com/docs/recipes/express/) or [FastAPI](https://www.screenlyyid.com/docs/recipes/fastapi/) recipes, which have the same two routes.
- **`lib/`**: the Flutter app. It contains no API key. It asks the back end to create a verification, asks for camera permission, opens the `webEnrolmentUrl` in a `WebViewWidget`, listens for `IDV_SESSION_COMPLETE` and `ERROR`, then asks the back end for the result.

Follow the steps and the camera-permission notes on the [Mobile app](https://www.screenlyyid.com/docs/integrations/mobile-app/) page. The example's README explains how to run both parts on a simulator, an emulator or a real phone.

## The details that matter in Flutter

These are the usual reasons capture "does nothing" in a Flutter WebView:

1. **Answer the page's camera request.** The capture page asks the WebView for the camera, and the WebView only provides it if you handle `onPermissionRequest` when you create the `WebViewController`. Holding the camera permission in your app is not enough. Grant it only if the app holds the permission.
2. **Forward the page's events yourself.** The hosted page sends `IDV_SESSION_COMPLETE` and `ERROR` with `window.parent.postMessage`, and a WebView has no outer page to receive them. Register a JavaScript channel with `addJavaScriptChannel`, then inject a `message` listener that calls it when the page has loaded.
3. **Platform settings.** On iOS, create the controller with `allowsInlineMediaPlayback: true` so the camera preview plays inside the page, and add `NSCameraUsageDescription` to `Info.plist`. On Android, declare `CAMERA` in the manifest and set `compileSdk` to 37 (`permission_handler` needs it). With CocoaPods rather than Swift Package Manager, also add `PERMISSION_CAMERA=1` to the Podfile's `GCC_PREPROCESSOR_DEFINITIONS`, or the iOS camera permission is always denied.

## The pieces to copy

| What | File in the example |
|---|---|
| Ask the back end for a session and for the result (the `http` package) | `lib/backend.dart` |
| Ask for camera permission before loading the page, send the user to Settings if it was denied for good | `lib/home_screen.dart` |
| `WebViewController` setup, the camera grant, the event channel, and only loading ScreenlyyID pages | `lib/verification_screen.dart` |
| One request for the result after `IDV_SESSION_COMPLETE` | `lib/result_screen.dart` |
| Android manifest permissions and the demo-only HTTP allowance for the local back end | `android/app/src/main/AndroidManifest.xml`, `android/app/src/main/res/xml/network_security_config.xml` |
| iOS camera usage text and the demo-only local-network allowance | `ios/Runner/Info.plist` |
| The server routes | `backend/server.mjs` |

## Run it

1. Create a `test_` API key in the dashboard under **Integrations**.
2. Start the back end: `cd backend && SCREENLYYID_API_KEY=test_... node server.mjs`.
3. Run the app with `flutter pub get` then `flutter run`. The Android emulator reaches the back end at `http://10.0.2.2:3000` and the iOS simulator at `http://localhost:3000` (both are the defaults). On a real phone, pass your computer's address: `flutter run --dart-define=BACKEND_URL=http://192.168.1.20:3000`.

The camera needs a real device on iOS. Plain HTTP to the local back end is allowed for the demo only: a real back end uses HTTPS, so remove the Android `network_security_config.xml` and the iOS `NSAllowsLocalNetworking` setting.
