# Flutter

{% hint style="info" %}
Hackle Flutter SDK는 Flutter SDK 2.0.0 이상을 지원합니다.

Flutter SDK는 [Android SDK](https://docs.hackle.io/development-guide/android), [iOS SDK](https://docs.hackle.io/development-guide/ios) 를 기반으로 작동합니다. 아래 OS를 지원합니다.

* Android API 16 (4.1 Jelly Bean) 이상
* iOS 13 이상
  {% endhint %}

{% hint style="info" %}
Hackle Flutter SDK 2.29.0 버전부터 iOS 최소 지원 버전이 iOS 13으로 상향되였습니다.

Hackle Flutter SDK 2.29.0 미만 버전의 최소 지원 버전은 iOS 10 입니다.
{% endhint %}

## SDK 의존성 추가

[![](https://img.shields.io/pub/v/hackle)](https://pub.dev/packages/hackle)

SDK 의존성을 추가합니다.

{% tabs %}
{% tab title="CLI" %}

```bash
flutter pub add hackle
```

{% endtab %}

{% tab title="pubspec.yaml" %}

```yaml
dependencies:
	hackle: ^2.7.0
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
SDK 설치/업데이트 후 앱을 새롭게 빌드해야 연동이 변경사항이 적용됩니다.
{% endhint %}

## SDK 초기화

SDK를 사용하기 위해서 반드시 `HackleApp`을 초기화 해야 합니다. `HackleApp`을 초기화 하기 위해 SDK 키가 필요합니다.

* `HackleApp`은 SDK의 기능을 사용하기 위한 메소드들을 제공하는 클래스입니다.
* SDK 키는 핵클 서비스의 대시보드 안에 위치한 [SDK 연동 정보](https://dashboard.hackle.io/config/sdk-setting)에서 확인하실 수 있습니다.

초기화 시 핵클 서버로부터 필요한 정보들을 가져와서 SDK에 저장합니다.

초기화는 비동기로 실행되며, await으로 초기화 완료 전까지 대기할 수 있습니다.

{% hint style="warning" %}
초기화가 완료 되기 전에 A/B 테스트, 기능 플래그를 호출하면 기본 그룹(A), 꺼짐(false)을 리턴합니다.
{% endhint %}

```dart
import "package:hackle/hackle.dart";

await HackleApp.initialize(YOUR_APP_SDK_KEY);
```

#### 권장 초기화 전략: 스플레시 화면을 통한 초기화

앱을 즉시 시작하지 않고 스플레시 화면을 표시하고 SDK를 초기화합니다. 이후 콜백을 통해 스플레시 화면을 닫고 사용자가 앱과 상호작용을 시작할 수 있도록 합니다.

### 초기화 시 사용자 주입

유저 정보를 포함하여 SDK를 초기화 할 수 있습니다.

* 유저 정보를 포함하지 않으면 로컬 스토리지에 저장된 유저 정보를 사용합니다.
* 유저 정보를 포함하는 경우 로컬 스토리지에 저장된 유저 정보는 사용하지 않습니다.
* 사용자 주입을 하지 않고, 로컬 스토리지에 저장된 유저 정보도 없는 경우 Hackle Device ID를 device id로 가지고 유저를 사용합니다.

{% hint style="info" %}
유저 정보는 SDK 초기화 이후에도 유저 정보 설정 함수를 통해 자유롭게 수정 할 수 있습니다.
{% endhint %}

{% hint style="warning" %}
초기화 시 주입한 유저 정보와 로컬 스토리지에 저장된 유저 정보는 병합하지 않습니다.

ex) 스토리지에 userId: A 가 저장된 상태에서 초기화 시 deviceId: B 를 주입하는 경우, userId: null, deviceId: B인 유저로 설정됩니다.
{% endhint %}

```dart
import "package:hackle/hackle.dart";

HackleConfig config = HackleConfigBuilder()
  .build();

HackleUser user = HackleUserBuilder()
  .userId("YOUR_USER_ID")
  .build();

await HackleApp.initialize(YOUT_APP_SDK_KEY, hackleConfig: config, user: user);
```

### 초기화 설정정보

SDK 초기화 시 몇 가지 설정을 추가할 수 있습니다.

```dart
import "package:hackle/hackle.dart";

HackleConfig config = HackleConfigBuilder()
  .build();

await HackleApp.initialize(YOUT_APP_SDK_KEY, hackleConfig: config);
```

<table data-full-width="false"><thead><tr><th width="223.26171875">설정</th><th width="295.48828125">기능</th><th width="125.8125">기본값</th><th width="99.55859375">지원 버전</th></tr></thead><tbody><tr><td><code>exposureEventDedupIntervalMillis</code></td><td><p>동일한 사용자가 연속으로 발생시킨 동일한 A/B 테스트, 기능플래그 분배결과에 대한 노출 이벤트를 제거합니다.</p><ul><li>최솟값: 1000 (1초)</li><li>최댓값: 3600000 (1시간)</li></ul></td><td>-1 (중복제거 하지 않음)</td><td>모든 버전</td></tr><tr><td><code>debug</code></td><td>모든 기능에 대한 로그를 콘솔에 출력하고, 이벤트를 즉시 전송합니다.</td><td><code>false</code></td><td>2.1.0+</td></tr><tr><td><code>pollingIntervalMillis</code></td><td><p>대시보드에서 설정한 정보를 주기적으로 업데이트 할 수 있습니다.</p><ul><li>최솟값 : 60000 (60초)</li></ul></td><td>-1 (주기적으로 업데이트하지 않음)</td><td>2.3.0+</td></tr><tr><td><code>automaticScreenTracking</code></td><td>Android / iOS 단에서 발생하는 Activity / ViewController의 변경을 감지해서 화면 변화를 추적합니다.</td><td><code>true</code></td><td>2.21.0+</td></tr><tr><td><code>sessionPolicy</code></td><td>세션 유지 조건과 만료 조건을 설정합니다.</td><td><p><code>ALWAYS_NEW_SESSION</code> ,</p><p><code>1800000</code></p></td><td>2.30.0+</td></tr><tr><td><code>optOutTracking</code></td><td>옵트아웃 활성화 여부.</td><td><code>false</code></td><td>2.30.0+</td></tr><tr><td><code>sessionTimeoutMillis</code> (deprecated)</td><td>세션만료 시간을 설정합니다. <code>sessionPolicy</code>를 사용해 주세요.</td><td>1800000 (30분)</td><td>2.9.0+</td></tr></tbody></table>

#### 세션 정책 설정

세션 정책을 설정하여 세션의 유지 조건과 만료 조건을 제어할 수 있습니다.

```dart
import "package:hackle/hackle.dart";

HackleSessionPolicy sessionPolicy = HackleSessionPolicy.builder()
    .persistCondition(HackleSessionPersistCondition.nullToUserId)
    .timeoutCondition(
        HackleSessionTimeoutCondition.builder()
            .timeoutMillis(3600000)
            .onForeground(false)
            .onBackground(true)
            .onApplicationStateChange(true)
            .build()
    )
    .build();

HackleConfig config = HackleConfigBuilder()
    .sessionPolicy(sessionPolicy)
    .build();

await HackleApp.initialize(YOUR_APP_SDK_KEY, hackleConfig: config);
```

### 대시보드 설정 정보 갱신

대시보드 설정 정보를 명시적으로 갱신 할 수 있습니다.

{% hint style="warning" %}
해당 함수는 60초에 한번 제한적으로 호출할 수 있습니다.
{% endhint %}

```dart
HackleApp.fetch();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hackle.io/development-guide/flutter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
