Skip to navigation

Flutter integration

Learn how to integrate the HUMAN Mobile SDK into a Flutter app.

This guide is relevant for the current Mobile SDK. While we highly recommend using the latest version, if you are using a legacy integration, refer to the iOS or Android documentation.

You can integrate the SDK into a Flutter project. There is no HUMAN Flutter package. Call the native SDK from Dart through a method channel.

Start the SDK

When starting the SDK, keep the following in mind:

  1. Start the SDK as soon as possible on the main thread. We recommend doing so in the Application’s onCreate function on Android or the AppDelegate’s didFinishLaunchingWithOptions function on iOS. If your app sends a URL request to your server before the SDK starts, the request will not include the SDK’s HTTP headers. As a result:
    • The Enforcer may block the request, and
    • The SDK will not be able to present a challenge to the user
  2. Create a HumanManager that handles calls from Flutter. On Android, register it on the FlutterEngine. On iOS, set up the method channel.

Below are example implementations for Android and iOS:

Don’t forget to replace <APP_ID> with your actual Application ID.

import android.app.Application
class MainApplication: Application() {
override fun onCreate() {
super.onCreate()
HumanManager.start(this)
}
}

In particular, note the following:

  1. Initialization: We start the SDK as early as possible on the main thread.
  2. Policy configuration: We create an HSPolicy instance and set HSAutomaticInterceptorPolicy/interceptorType property to HSAutomaticInterceptorType/none. That disables Automatic Interception, which lets the SDK manipulate URL requests and handle their responses, but isn’t supported in Flutter. If the app loads a HUMAN-protected site in webview_flutter, also set hybridAppPolicy.webRootDomains and hybridAppPolicy.supportExternalWebViews = true before start. There is no HUMAN Flutter package that turns that flag on for you. See WebView container support for more information.
  3. Starting the SDK: We call HumanSecurity/start(appId:policy:) with the following parameters:
    • The Application instance (Android only)
    • Your Application ID. If you have multiple IDs, see our guide on how to handle them.
    • The HSPolicy object that we configured

Send your requests through the SDK and check for a blocked request

  1. In your Dart code, create a MethodChannel.
  2. Add the SDK’s HTTP headers to the URL request.
  3. When a request is blocked, send the block response to the SDK.
  4. Handle the challenge’s result. See handle blocked requests for more information.

Below is an example implementation:

Dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../settings/settings_view.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
class MyView extends StatelessWidget {
static const humanPlatform = MethodChannel('com.humansecurity/sdk');
Future<void> sendUrlRequest() async {
try {
final String humanHeaders = await humanPlatform.invokeMethod('humanGetHeaders');
final Map<String, String> humanHeadersMap = Map.from(json.decode(humanHeaders));
var url = Uri.https('example.com', '/action');
var response = await http.get(url, headers: humanHeadersMap);
if (response.statusCode == 403) {
final String result = await humanPlatform.invokeMethod('humanHandleResponse', response.body);
if (result == 'solved') {
print("Challenge was solved");
} else if (result == 'false') {
print("Request was not blocked by HUMAN");
} else if (result == 'cancelled') {
print("Challenge was cancelled");
}
}
} on PlatformException catch (exception) {
print("Exception: '${exception.message}'");
}
}
}

Handle blocked requests

When the Enforcer blocks a request, it returns a JSON string in the response body with an HTTP status code of 403, which includes metadata for the SDK. For example:

{
"vid": "928d7ab3-9cf1-11ee-a624-b802520f369f",
"uuid": "fd01e6d6-9cf2-11ee-808c-acde48001122",
"page": "...",
"appId": "PXj9y4Q8Em",
"action": "captcha",
"collectorUrl": "https://collector-pxj9y4q8em.perimeterx.net"
}

Your app should pass the whole JSON to the SDK via the HSBotDefender/handleResponse(response:data:callback:) function. If not, the SDK won’t present a challenge to the user.