Flutter

Integrated Requirement

Compliance Notice

The privacy policy of apps that integrate the TDBehavior module should be updated based on our Privacy Agreement with the following additional information:

(1) Function Description: To identify machine behavior fraud risks, the SDK needs to obtain relevant behavior information from you or your end users.

(2) Necessary Personal Information: Touch, scroll, swipe, screen edge swipe, scale behavior information; input behavior information (input type, number of times, input speed, input time); application foreground/background behavior information; browsing page names and dwell time behavior information; device orientation behavior information; gyroscope sensor behavior information; accelerometer sensor behavior information; mouse behavior information (web); keyboard behavior information (web)

(3) Other Optional Personal Information: None


1.SDK Integration

SDK Integration

  • Add the dependency in *pubspec.yaml

    dependencies:
      tdbehavior_flutter: ^1.4.5
  • Run flutter pub get to install dependencies


2.Configuring in Flutter code

1. Wrap Root Widget

Use TDBehaviorWidget to wrap your application in main()

import 'package:flutter/material.dart';
import 'package:trustdevice_pro_plugin/trustdevice_pro_plugin.dart';
import 'package:tdbehavior_flutter/tdbehavior_flutter.dart';

void main() {
  runApp(
    TDBehaviorWidget(
      key: const Key("root_layout"),
      child: MaterialApp( // or CupertinoApp
        navigatorObservers: [
          // Add route observer for tracking
          TDBehaviorWidget.getRouteObserver(),
          // Other custom observers...
        ],
        title: 'Your App Name',
        home: const MyHomePage(title: 'Flutter Demo Home Page'),
      ),
    ),
  );
}

2.Text Input Monitoring

The SDK automatically monitors input controls with Key. TextFields to be tracked need to have a unique Key:

TextField(
  key: const Key('username_field'),
  controller: _usernameController,
  decoration: const InputDecoration(
    labelText: 'Username',
    hintText: 'Please enter username',
  ),
);

3.Initialization

⚠️⚠️⚠️If device fingerprinting is already integrated, you should register TDBehavior module with the device fingerprinting; otherwise, the behavior collection module will not be usable.

Sample Code

class _MyHomePageState extends State<MyHomePage> {
  late TrustdeviceProPlugin _trustdeviceProPlugin;
  @override
  void initState() {
    super.initState();

    // ⚠️⚠️⚠️register behavior for tracking
    final behaviorCollector = TDBehavior();
    _trustdeviceProPlugin = TrustdeviceProPlugin(behaviorCollector);

  }

}

4.Call behavior collect methods

Behavior collect methods are used to obtain user behavior data. The interface definition is as follows:

/*** TDBehavior start, start behavior collect, write collected data into SDK internal container
*/
void start()   

/*** TDBehavior stop, end behavior collect, stop writing collected data into SDK internal container and clear the data in SDK internal container;
*/
void stop()

/*** TDBehavior collect, return behavior collect result, if data exists in SDK internal container, it will return success code == 0, and clear the data in SDK internal container;
* If no data exists in SDK internal container, it will return code == 104 error code
*/
Futurn<Map<String, dynamic>> collect()

Sample Code

⚠️⚠️⚠️ Ensure that there is user behavior after calling start in the business interface, such as text input, click, swipe, etc. Otherwise, calling collect will return a 104 error code, indicating that no behavior data was collected;

    // When entering business interface, call start method to perform behavior collect, for example when entering login interface    
    _trustdeviceProPlugin.start();    
    ...    
    // When calling business interface, call collect method to get behavior collect data, for example when clicking login button    
    final result = await _trustdeviceProPlugin.collect();
    final int code = result['code'];
    final String msg = result['msg'];
    final String payload = result['payload'];

    if (code == 0) {
        print('Get behavior collect result successfully, payload: $payload');
    } else {
        print('Get behavior collect result failed, code: $code, msg: $msg');
    }   
    ...    
    // When leaving business interface, call stop method to end behavior collect, for example when leaving login interface    
    _trustdeviceProPlugin.stop();

Specify input fields that do not require collected

Set the attribute key including "TDBehaviorExInput" in TextField. After this setting, the text input events of TextField will no longer be collected

        const TextField(
          key: "otherKey,TDBehaviorExInput",
          decoration: InputDecoration(
            labelText: 'will not collect TextField text input'
          )
        )

initWithOptions method Optional Configuration

KeyDefinitionDescriptionSample code
behaviorHttpTimeOutTime out for all network requests within TDBehavior SDKUnit: seconds, floatDefault: 15.0options['behaviorHttpTimeOut'] = 15.0;

5.Return Codes

Error CodeDescriptionSolution
0SuccessNo action needed
100initWithOptions not calledinitWithOptions not called, need to call initWithOptions
101Network authentication result not returned due to low network speedThrow an error to the user: "Network failure, please check the network and try again", guiding the user to call the collect method again
102Network error in authentication interface due to system reasons, such as no network connectionThrow an error to the user: "Network failure, please check the network and try again", guiding the user to call the collect method again
103Authentication interface returns status code is not 200Check if partner parameters are correct. If still fails, provide error message resultStruct.msg to our operations team to confirm if parameters are correct and within validity period
104No data in SDK internal containerThere is no valid collect data,Please Ensure that there is user behavior after calling start in the business interface, such as text input, click, swipe, etc!
109No data in SDK internal containerThere is no valid collect data,Switches are all NO,Please contact TrustDecision!
110No data in SDK internal containerThere is no valid collect data,Please call the start method first!