Integration with Flutter
  • 02 May 2023
  • Dark
    Light

Integration with Flutter

  • Dark
    Light

Article Summary

v3.0

The SDK can be integrated into Flutter 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:

<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:

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:

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

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

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:

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}'.");
    }
}

Was this article helpful?