Flutter integration
Learn how to integrate the HUMAN Mobile SDK into a Flutter app.
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:
- Start the SDK as soon as possible on the main thread. We recommend doing so in the
Application’sonCreatefunction on Android or theAppDelegate’sdidFinishLaunchingWithOptionsfunction 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
- Create a
HumanManagerthat handles calls from Flutter. On Android, register it on theFlutterEngine. 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.
Android
iOS
In particular, note the following:
- Initialization: We start the SDK as early as possible on the main thread.
- Policy configuration: We create an
HSPolicyinstance and setHSAutomaticInterceptorPolicy/interceptorTypeproperty toHSAutomaticInterceptorType/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 inwebview_flutter, also sethybridAppPolicy.webRootDomainsandhybridAppPolicy.supportExternalWebViews = truebefore start. There is no HUMAN Flutter package that turns that flag on for you. See WebView container support for more information. - Starting the SDK: We call
HumanSecurity/start(appId:policy:)with the following parameters:- The
Applicationinstance (Android only) - Your Application ID. If you have multiple IDs, see our guide on how to handle them.
- The
HSPolicyobject that we configured
- The
Send your requests through the SDK and check for a blocked request
- In your Dart code, create a
MethodChannel. - Add the SDK’s HTTP headers to the URL request.
- When a request is blocked, send the block response to the SDK.
- Handle the challenge’s result. See handle blocked requests for more information.
Below is an example implementation:
Dart
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:
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.