> For the complete documentation index, see [llms.txt](https://docs.sentiance.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sentiance.com/getting-started/sdk-integration/3.-sdk-initialization/flutter.md).

# 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 <mark style="color:$success;">**`await SentianceCore.ensureInitialized()`**</mark> before your first SDK call and handle a possible initialization failure.

## Steps

{% stepper %}
{% step %}

### Initialize the Sentiance SDK for iOS

Make sure the initialization is triggered before the <mark style="color:$success;">**`didFinishLaunchingWithOptions`**</mark> method returns.

1. Open <mark style="color:$success;">**`ios/Runner/AppDelegate.swift`**</mark>
2. Import the <mark style="color:$success;">**`sentiance_core`**</mark> package
3. Call the <mark style="color:$success;">**`initializeAsync`**</mark> method provided by the <mark style="color:$success;">**`SentianceCorePlugin`**</mark> class
4. Log the outcome from the completion handler

{% code title="AppDelegate.swift" %}

```swift
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)
  }
}
```

{% endcode %}
{% endstep %}

{% step %}

### Initialize the Sentiance SDK for Android

1. 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)
2. Locate the <mark style="color:$success;">**`onCreate`**</mark> method
3. Import the Sentiance core plugin
4. Call the static <mark style="color:$success;">**`initializeAsync`**</mark> method on the <mark style="color:$success;">**`CorePlugin`**</mark> class
5. Log the outcome from the completion listener

{% code title="MainApplication.kt" %}

```kotlin
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}") }
                }
            }
    }
}

```

{% endcode %}

Run the app, open a new Terminal window and verify logs by running the command:

```cmd
adb logcat | grep Sentiance
```

{% endstep %}

{% step %}

### Ensure the SDK is initialized from Dart code

Because initialization is asynchronous, <mark style="color:$success;">**`await SentianceCore.ensureInitialized()`**</mark> before your first SDK call. It completes once initialization finishes, and throws a <mark style="color:$primary;">**`SdkInitializationError`**</mark> if initialization failed or was never triggered.

{% code title="app.dart" %}

```dart
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}');
}
```

{% endcode %}
{% endstep %}

{% step %}

### 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](/getting-started/sdk-integration/4.-user-creation.md).
{% endstep %}
{% endstepper %}
