Flutter環境構築

# Flutterのインストール

```zsh
❯ brew install --cask flutter
```

Homebrew経由でインストールしてるので自動的にpathが通ってるはずです
```zsh
❯ flutter --version
Flutter 3.24.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 2663184aa7 (3 weeks ago) • 2024-09-11 16:27:48 -0500
Engine • revision 36335019a8
Tools • Dart 3.5.3 • DevTools 2.37.3
```

# 環境診断ツール ( flutter doctor ) を実行する

```zsh
❯ flutter doctor
```
上記コマンドを実行した結果、以下が出力されました
```zsh
❯ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.24.3, on macOS 14.2 23C64 darwin-arm64, locale ja-JP)
[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/to/macos-android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[✗] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS and macOS development.
      Download at: https://developer.apple.com/xcode/
      Or install Xcode via the App Store.
      Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
        sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods not installed.
        CocoaPods is a package manager for iOS or macOS platform code.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/to/platform-plugins
      For installation instructions, see https://guides.cocoapods.org/using/getting-started.html#installation
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] VS Code (version 1.93.1)
[✓] Connected device (2 available)
[✓] Network resources

! Doctor found issues in 3 categories.
```

現時点での環境構築において完了しているものが `[✓]` で出力され、足りてないものが `[✗]` もしくは `[!]` として出力されます

ということで `flutter doctor` の出力結果をもとに、それぞれ対応していきます

### Android toolchain - develop for Android devices

以下の対応ですね
```zsh
[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/to/macos-android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.
```

`flutter doctor` で出力されている通り、私の場合は Android Studio をそもそもインストールできてなかったのでインストールします
```
[!] Android Studio (not installed)
```

1. [Android Studio](https://developer.android.com/studio/?gclid=Cj0KCQiAjJOQBhCkARIsAEKMtO3zEhdK4_I0CEZic3UH4dl-9gVXuHFR9dCl3TOHKjmv3xWLU3UxfhYaApfAEALw_wcB&gclsrc=aw.ds&hl=ja) のインストール
2. More Actions > SDK Manager を開く
![スクリーンショット 2024-10-03 11.07.26.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3900714/67e30cce-83aa-995e-ce69-5b6695f4543a.png)
3. Android SDK Command-line Tools (latest) にチェックを入れて Apply をクリック
![スクリーンショット 2024-10-03 11.09.49.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3900714/d6f2c2f0-cc1d-73a3-c559-a4fc38e5ce9f.png)
4. 上記ダウンロードが完了したら OK をクリック

改めて `flutter doctor` を実行します

```zsh
❯ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.24.3, on macOS 14.2 23C64 darwin-arm64, locale ja-JP)
[!] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    ! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[✗] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS and macOS development.
      Download at: https://developer.apple.com/xcode/
      Or install Xcode via the App Store.
      Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
        sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods not installed.
        CocoaPods is a package manager for iOS or macOS platform code.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/to/platform-plugins
      For installation instructions, see https://guides.cocoapods.org/using/getting-started.html#installation
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.1)
[✓] VS Code (version 1.93.1)
[✓] Connected device (2 available)
[✓] Network resources

! Doctor found issues in 2 categories.
```

別のエラーメッセージが表示されていますね
```zsh
[!] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    ! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
```

### Some Android licenses not accepted.

`To resolve this, run: flutter doctor --android-licenses` と言われているので従います。

何度か質問されるので全て y を入力します
```
All SDK package licenses accepted
```

エラーが解消されましたね
```zsh
❯ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.24.3, on macOS 14.2 23C64 darwin-arm64, locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
[✗] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS and macOS development.
      Download at: https://developer.apple.com/xcode/
      Or install Xcode via the App Store.
      Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
        sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods not installed.
        CocoaPods is a package manager for iOS or macOS platform code.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/to/platform-plugins
      For installation instructions, see https://guides.cocoapods.org/using/getting-started.html#installation
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.1)
[✓] VS Code (version 1.93.1)
[✓] Connected device (2 available)
[✓] Network resources

! Doctor found issues in 1 category.
```

次に進みますよ

### Xcode installation is incomplete; a full installation is necessary for iOS

以下の対応です
```zsh
[✗] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS and macOS development.
      Download at: https://developer.apple.com/xcode/
      Or install Xcode via the App Store.
```

私は Xcode をインストールしておらず、それが原因ですのでインストールしていきます
私の場合は [Appleの開発者向けサイト](https://developer.apple.com/download/all/) からダウンロードしました

ダウンロード完了後、`flutter doctor` で出力されている通り以下のコマンドを実行
```zsh
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
sudo xcodebuild -runFirstLaunch
```

該当のエラーメッセージは消えましたね
```zsh
❯ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.24.3, on macOS 14.7 23H124 darwin-arm64, locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
[!] Xcode - develop for iOS and macOS (Xcode 16.0)
    ✗ CocoaPods not installed.
        CocoaPods is a package manager for iOS or macOS platform code.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/to/platform-plugins
      For installation instructions, see https://guides.cocoapods.org/using/getting-started.html#installation
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.1)
[✓] VS Code (version 1.93.1)
[✓] Connected device (3 available)
[✓] Network resources

! Doctor found issues in 1 category.
```

残るエラーメッセージはラスト1つですね

### CocoaPods not installed.

今回は CocoaPods を Homebrew 経由でインストールします

```zsh
❯ brew install cocoapods
```

これで `flutter doctor` が通りましたね！

```zsh
❯ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.24.3, on macOS 14.7 23H124 darwin-arm64, locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.1)
[✓] VS Code (version 1.93.1)
[✓] Connected device (3 available)
[✓] Network resources

• No issues found!
```

`No issues found!` になりました

Flutter でのアプリ開発を進めていきましょう🎉