# Flutter

## サポート環境

### Android

* Android API level: 26 以上
* Kotlin: 1.9.10 以上

### iOS

* iOS 15.0 以上
* Xcode 16.1 以上
* Swift 5.9 以上

***

## Step 1. パッケージをインストールする

<https://pub.dev/packages/nubrick_flutter> にパッケージが公開されています。

以下のコマンドを実行してパッケージをインストールしてください。

{% code title="Terminal" %}

```bash
flutter pub add nubrick_flutter
```

{% endcode %}

または、直接`pubspec.yaml`を編集し、`pub get`を実行することでもインストールできます。

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

```yaml
dependencies:
  nubrick_flutter: <LATEST_VERSION>
```

{% endcode %}

{% code title="Terminal" %}

```bash
flutter pub get
```

{% endcode %}

***

## Step 2. Nubrick SDKを初期化する

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

```dart
import 'package:nubrick_flutter/nubrick_flutter.dart';
import 'package:nubrick_flutter/provider.dart';
import 'package:nubrick_flutter/user.dart';

// ...

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  // Initialize Nubrick Client
  Nubrick("<PROJECT_ID>");

  // Run your app
  runApp(const MyApp());
}

class _YourAppState extends State<YourApp> {
  @override
  void initState() {
    if (!mounted) return;

    // Set user's properties to use them in the nubrick experiment
    final user = NubrickUser();
    await user.setProperties({
      'prefecture': "Tokyo",
      'environment': const bool.fromEnvironment('dart.vm.product')
          ? 'production'
          : 'development',
    });
  }

  @override
  Widget build(BuildContext context) {
    // Add NubrickProvider to your root widget
    return NubrickProvider(
      child: MaterialApp(
        // ...
      ),
    );
  }
}
```

{% endcode %}

## Step 3.a (Android only) Set up Platform Specific Code

To get it working on Android, you need to make a small change to your MainActivity.kt file.

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

```kt
package com.example.app

import io.flutter.embedding.android.FlutterFragmentActivity

// 1. Extend FlutterFragmentActivity instead of FlutterActivity
class MainActivity: FlutterFragmentActivity()
```

{% endcode %}

and, set the minimum android sdk version to 26 in your android/app/build.gradle file.

{% code title="android/app/build.gradle" %}

```gradle
android {
    defaultConfig {
        // Set the minimum sdk version to 26
        minSdkVersion 26
    }
}
```

{% endcode %}

finally, set the kotlin version to >= 1.9.10 in your settings.gradle file.

{% code title="settings.gradle" %}

```gradle
plugins {
    // ...
    id "org.jetbrains.kotlin.android" version "1.9.10" apply false
}
```

{% endcode %}

## Step 3.b. (iOS only) Set up Platform Specific Code

To get it working on iOS, you need to change a minimum deployments to 15.0 in your ios/Runner.xcodeproj/project.pbxproj file.

1. In Xcode, navigate to `Runner.xcodeproj > TARGETS > Runner > General > Minimum Deployments`
2. Change the `minimum deployments` to `15.0`

Once you're finished, you can start using Nubrick.
