macOS に Flutter の開発環境を構築する

<div align="center">
<img src="https://qiita-image-store.s3.amazonaws.com/0/33505/de4d52df-7854-b64c-fe2d-7979ec0a79b6.png">
</div>

# 目標
Flutter を macOS に構築する
基本的には https://flutter.io/setup-macos/ を参考に

# システム要件
* OS: macOS (64-bit)
* ストレージ: 700 MB (IDE/tools除く)
* 依存関係のあるツール: bash, mkdir, rm, git, curl, unzip, which

# 構築手順

## 1. Flutter SDK をダウンロード
方法は２とおり

#### 1-1. zip
https://flutter.io/sdk-archive/#macos からSDKをダウンロード
今回は [flutter_macos_v0.3.2-beta.zip](https://storage.googleapis.com/flutter_infra/releases/beta/macos/flutter_macos_v0.3.2-beta.zip) を使います

```bash
$ cd ~/development
$ unzip ~/Downloads/flutter_macos_v0.3.2-beta.zip
$ ./flutter/bin/flutter --version
Flutter 0.3.2 • channel beta • https://github.com/flutter/flutter.git
Framework • revision 44b7e7d3f4 (4 weeks ago) • 2018-04-20 01:02:44 -0700
Engine • revision 09d05a3891
Tools • Dart 2.0.0-dev.48.0.flutter-fe606f890b
```

#### 1-2. git clone
```bash
$ git clone -b master https://github.com/flutter/flutter.git

$ ./flutter/bin/flutter --version
 :
Flutter 0.4.5-pre.21 • channel master • https://github.com/flutter/flutter.git
Framework • revision f38743593d (2 hours ago) • 2018-05-15 19:37:21 -0700
Engine • revision 06afdfe54e
Tools • Dart 2.0.0-dev.54.0.flutter-46ab040e58
```

## 2. Flutter の依存関係を確認
```bash
$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
 :
```

出てきた内容ごとに対応していく

---
```bash
[!] Android toolchain - develop for Android devices (Android SDK 27.0.3)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses
```

ライセンスの問題だけだったのでそのまま実行

```bash
$ flutter doctor --android-licenses
 :
Accept? (y/N): y
All SDK package licenses accepted
```

---
```bash
[!] iOS toolchain - develop for iOS devices (Xcode 9.3.1)
    ✗ libimobiledevice and ideviceinstaller are not installed. To install, run:
        brew install --HEAD libimobiledevice
        brew install ideviceinstaller
    ✗ ios-deploy not installed. To install:
        brew install ios-deploy
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
        For more info, see https://flutter.io/platform-plugins
      To install:
        brew install cocoapods
        pod setup
 :
```
上記そのまま対応する

```bash
$ brew install --HEAD libimobiledevice
$ brew install ideviceinstaller
$ brew install ios-deploy
$ brew install cocoapods
$ pod setup
```

もう一度 `$ flutter doctor` をしても次のエラーでたら

```bash
[!] iOS toolchain - develop for iOS devices (Xcode 9.3.1)
    ✗ Missing Xcode dependency: Python module "six".
      Install via 'pip install six' or 'sudo easy_install six'.
```

まずは

```bash
$ pip install six
Requirement already satisfied: six in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (1.4.1)
matplotlib 1.3.1 requires nose, which is not installed.
matplotlib 1.3.1 requires tornado, which is not installed.
```

となったら

```bash
$ brew reinstall python@2
 :
$ pip install six
Collecting six
  Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Installing collected packages: six
Successfully installed six-1.11.0
```

で、OK

---
```bash
[✓] Android Studio (version 3.1)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
```
Android Studio から **_Preference... > Plugins > Browse repositories..._** で Flutter と Dart をインストール

---
```bash
[!] Connected devices
    ! No devices available
```
　エミュレータかデバイスを接続するとOKなのでこれはこのまま

---

全部成功するとこんな感じになる

```bash
$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, v0.3.2, on Mac OS X 10.13.3 17D47, locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK 27.0.3)
[✓] iOS toolchain - develop for iOS devices (Xcode 9.3.1)
[✓] Android Studio (version 3.1)
[✓] VS Code (version 1.23.1)
[!] Connected devices
    ! No devices available

! Doctor found issues in 1 category.
```

## $PATH の設定
.bash_profile に `$PATH` を追加する

```bash
$ cat .bash_profile
export PATH=`pwd`/development/flutter/bin:$PATH
```

# 動作チェック

**AndroidエミュレータとiPhoneシミュレータを起動**

```bash
$ ./Library/Android/sdk/tools/emulator -list-avds
Nexus_5X_API_26 // なかったらAVDでエミュレータを作成しておく

$ ./Library/Android/sdk/tools/emulator @Nexus_5X_API_26
```

```bash
$ open -a Simulator.app
```

**Flutterアプリケーションの作成**

```bash
$ flutter create myapp
Creating project myapp...
 :
All done! In order to run your application, type:

  $ cd myapp
  $ flutter run

Your main program file is lib/main.dart in the myapp directory.
```

**アプリケーションの実行**

```bash
$ flutter run -d all
```

<img width="400" alt="スクリーンショット 2018-05-16 14.32.27.png" src="https://qiita-image-store.s3.amazonaws.com/0/33505/4f2eedde-e84d-62d1-cf76-8f25212e833d.png">



done.