Closed2

Flutter VM Serviceへの接続ができないiOS実機

kabikirakabikira

FlutterアプリのiOS実機デバッグ時の接続エラー解決方法

発生した問題

FlutterアプリをiOS実機でデバッグ実行しようとしたところ、ビルド自体は完了するものの、VM Serviceへの接続ができずにデバッグセッションが開始できない問題が発生しました。

以下のようなエラーが表示されました:

Launching lib/main.dart on iPhoneデバイス in debug mode...
Automatically signing iOS for device deployment using specified development team in Xcode project: XXXXXXXXXX
Xcode build done.                                           29.6s
You may be prompted to give access to control Xcode. Flutter uses Xcode to run your app. If access is not allowed, you can change this through your Settings > Privacy & Security > Automation.
Connecting to the VM Service is taking longer than expected...
Still attempting to connect to the VM Service...
If you do NOT see the Flutter application running, it might have crashed. The device logs (e.g. from adb or XCode) might have more details.
If you do see the Flutter application running on the device, try re-running with --host-vmservice-port to use a specific port known to be available.
Exception attempting to connect to the VM Service: SocketException: Connection refused (OS Error: Connection refused, errno = 61), address = 127.0.0.1, port = XXXXX
This was attempt #50. Will retry in 0:00:01.600000.

解決方法

エラーメッセージの提案通り、特定のポートを指定して実行することで問題が解決しました:

flutter run --host-vmservice-port=8888

なぜこれで解決したのか?

  1. ポート競合の回避: デフォルトでFlutterはランダムなポートを使用しますが、そのポートが他のプロセスで使用されていたり、ファイアウォールでブロックされていたりする可能性があります。

  2. 安定した接続: 特定のポート(8888)を明示的に指定することで、Flutter DartのVM Serviceとデバッガー間の通信経路が安定します。

  3. ファイアウォールとの互換性: セキュリティソフトウェアやファイアウォールが、ランダムなポートではなく固定ポートでの通信を許可しやすくなります。

応用

この問題が頻繁に発生する場合は、VSCodeなどのIDEの起動設定に--host-vmservice-port=8888を追加しておくと便利です。

// .vscode/launch.json の例
{
  "configurations": [
    {
      "name": "Flutter",
      "request": "launch",
      "type": "dart",
      "args": [
        "--host-vmservice-port=8888"
      ]
    }
  ]
}

注意点

  • 8888ポートが他のアプリケーションで使用されている場合は、別のポート番号(例:8889, 9000など)を試してみてください。
  • このポート指定はデバッグ時のみ必要で、リリースビルドには影響しません。
kabikirakabikira

【Flutter】iOS実機デバッグ時の「VM Serviceに接続できない」エラーの解決方法

このスクラップは5ヶ月前にクローズされました