Flutter
Initialization allows the SDK to perform detections in the background. You must always initialize the SDK before interacting with it. Only a limited set of SDK methods are safe to invoke on an uninitialized SDK.
Initialization is asynchronous. This means your Dart code can start running before the native SDK has finished initializing. Most SDK methods fail if called before initialization completes, so in Dart you must await SentianceCore.ensureInitialized() before your first SDK call and handle a possible initialization failure.
Steps
Initialize the Sentiance SDK for iOS
Make sure the initialization is triggered before the didFinishLaunchingWithOptions method returns.
Open
ios/Runner/AppDelegate.swiftImport the
sentiance_corepackageCall the
initializeAsyncmethod provided by theSentianceCorePluginclassLog the outcome from the completion handler
import Flutter
import UIKit
import sentiance_core
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
SentianceCorePlugin.shared.initializeAsync(launchOptions: launchOptions) { result, error in
if result != nil {
print("Sentiance SDK initialization succeeded")
} else {
print("Sentiance SDK initialization failed, reason: \(String(describing: error?.failureReason))")
}
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}Initialize the Sentiance SDK for Android
Open the file where your Application class resides, or create a new Application class (make sure to update AndroidManifest.xml to point to the newly created Application class)
Locate the
onCreatemethodImport the Sentiance core plugin
Call the static
initializeAsyncmethod on theCorePluginclassLog the outcome from the completion listener
package com.your.domain
import android.app.Application
import android.util.Log
import com.sentiance.core_plugin.CorePlugin
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
CorePlugin.initializeAsync(this)
.addOnCompleteListener { op ->
if (op.isSuccessful) {
Log.d("Sentiance", "Sentiance SDK initialization succeeded")
} else {
Log.e("Sentiance", "Sentiance SDK initialization failed, reason: ${op.error?.failureReason}")
op.error?.throwable?.let { Log.e("Sentiance", "throwable: ${it.message}") }
}
}
}
}
Run the app, open a new Terminal window and verify logs by running the command:
adb logcat | grep SentianceEnsure the SDK is initialized from Dart code
Because initialization is asynchronous, await SentianceCore.ensureInitialized() before your first SDK call. It completes once initialization finishes, and throws a SdkInitializationError if initialization failed or was never triggered.
import 'package:sentiance_core/sentiance_core.dart';
try {
await SentianceCore.ensureInitialized();
// The SDK is initialized. It is now safe to call other SDK methods.
} on SdkInitializationError catch (e) {
switch (e.reason) {
case SdkInitializationFailureReason.notTriggered:
// Init was never started, or was started late / off the main thread.
// Make sure initializeAsync runs synchronously from your native entry point.
break;
case SdkInitializationFailureReason.exceptionOrError:
// An error occurred during initialization, or the native reason was unrecognized.
break;
// Handle the other cases here
}
print('Sentiance SDK initialization failed: ${e.reason}, ${e.message}');
} on PlatformException catch (e) {
// An unexpected error occurred while talking to the host platform.
print('Unexpected error: ${e.message}');
}Next: User Creation
After having successfully initialized the Sentiance SDK, you must now create a user. To do so, follow the instructions on this page.
Last updated