> This page is for Applications Protection.

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.humansecurity.com/llms.txt.

# Integration with Flutter

#### v3.0

The SDK can be integrated into [Flutter](https://flutter.dev/) projects.

## Add Theme.AppCompat.Light style

The SDK requires that the `Theme.AppCompat.Light` style will be set as the application's theme. You should add it to the `AndroidManifest.xml` file:

```none
<application
    android:name=".MainApplication"
    android:theme="@style/Theme.AppCompat.Light"
</application>
```

## Start the SDK

The automatic interceptor is not supported in Flutter, so any request from the Dart code has to be handled manually. Here is an example:

```none
try {
    val policy = PXPolicy()
    policy.urlRequestInterceptionType = PXPolicyUrlRequestInterceptionType.NONE
    PerimeterX.start(application, "<APP_ID>", null, policy)
}
catch (exception: Exception) {
    println("failed to start. exception: $exception")
}
```

## Setup a channel

Configure the channel in Dart. Here is an example:

```none
static const perimeterxPlatform = MethodChannel('com.my.app/perimeterx');
```

Setup a channel in order to handle calls from the Dart code. Here is an example:

```none
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "com.perimeterx.demo/perimeterx").setMethodCallHandler {
                call, result ->
    if (call.method == "_getPxHeaders") {
        val json = JSONObject(PerimeterX.headersForURLRequest(null) as Map<*, *>?)
        result.success(json.toString())
    }
    else if (call.method == "_handlePxResponse") {
        val handled = PerimeterX.handleResponse(call.arguments!! as String, null) { challengeResult: PerimeterXChallengeResult ->
            result.success(if (challengeResult == PerimeterXChallengeResult.SOLVED) "solved" else "cancelled")
            null
        }
        if (!handled) {
            result.success("false")
        }
    }
    else {
        result.notImplemented()
    }
}
```

## Pass the SDK's HTTP headers to the Dart code and handle the response

In your Dart code, call those functions for every URL request to add those HTTP headers and handle the response. Here is an example:

```none
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

Future<void> _sendUrlRequest() async {
    try {
        final String pxHeaders = await perimeterxPlatform.invokeMethod('_getPxHeaders');
        final Map<String, String> pxHeadersMap = Map.from(json.decode(pxHeaders));
        print(pxHeadersMap);
        var url = Uri.https('my.request.com', '/my.path');
        var response = await http.get(url, headers: pxHeadersMap);
        if (response.statusCode == 403) {
            final String result = await perimeterxPlatform.invokeMethod('_handlePxResponse', response.body);
            print(result);
            /*
                check the result:
                'false' - not handled by the SDK
                'solved' - challenge solved
                'cancelled' - challenge cancelled
            */
            if (result == 'solved') {
                // challenge solved. you may retry the request here
                print("challenge solved");
            } else if (result == 'false') {
                // request finished successfully
                print("request finished successfully");
            } else if (result == 'cancelled') {
                // challenge cancelled
                print("challenge cancelled");
            }
        }
    } on PlatformException catch (e) {
        print("failed to get px's headers: '${e.message}'.");
    }
}
```