当前位置: 首页 > news >正文

Flutter 真机 Debug 报 VM Service 连接失败(Android / iOS 通用)— 代理环境变量导致

Flutter 真机 Debug 报 VM Service 连接失败(Android / iOS 通用)— 代理环境变量导致

现象

在 Android Studio / Xcode / flutter run(debug)时出现类似错误:

Error connecting to the service protocol

HttpException: Connection closed before full header was received

连接地址形如:http://127.0.0.1://ws

常见伴随:

flutter run --profile 可以正常运行

但 flutter run(debug)或 IDE 点击运行失败

iOS 可能出现 LLDB 停住、SIGSTOP / signal 9 等(根因仍可能是 VM service 握手失败引发的连锁反应)

根因

本机 shell 环境(例如 .zshrc)设置了:

export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890

或 all_proxy / http_proxy / https_proxy

导致 Flutter debug 所需的 localhost VM Service WebSocket(127.0.0.1) 也被代理接管,握手/headers 被中途截断,从而报错。

临时修复(终端可用)

在当前终端会话执行:

unset http_proxy https_proxy all_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY
export NO_PROXY="127.0.0.1,localhost,::1"
export no_proxy="$NO_PROXY"

再运行:

flutter run -v
永久修复(推荐)

在 ~/.zshrc(或 ~/.zprofile)中保留代理,但显式绕开 localhost:

export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890

export NO_PROXY="127.0.0.1,localhost,::1"
export no_proxy="$NO_PROXY"

执行:

source ~/.zshrc
Android Studio / IntelliJ 额外修复(IDE 点击运行仍失败时)

IDE 可能使用自己的代理配置或继承启动时环境变量,需在 IDE 内设置绕开 localhost:

路径:
Preferences/Settings → Appearance & Behavior → System Settings → HTTP Proxy

若启用代理:在 No proxy for 填入:

localhost,127.0.0.1,::1

或直接改为 No Proxy

重启 Android Studio 后生效。

验证方法

终端检查环境变量:

env | egrep -i 'http_proxy|https_proxy|all_proxy|no_proxy'

应满足:

NO_PROXY/no_proxy 包含 127.0.0.1,localhost,::1

Flutter debug 可正常连接 VM Service,IDE 点击运行也正常