You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter应用在VS Code调试控制台出现Failed host lookup错误

VS Code调试Flutter出现"Failed host lookup: '$host'"问题解决

问题场景

在VS Code调试控制台启动Flutter应用时,APK构建成功,但后续抛出Failed host lookup: '$host'错误;但在终端执行flutter run或Android Studio中启动同一项目则完全正常,且终端运行时会自动分配不同的VM Service端口。

VS Code调试控制台日志

Launching lib\main.dart on AOSP on IA Emulator in debug mode...
√  Built build\app\outputs\flutter-apk\app-debug.apk.
Connecting to VM Service at ws://127.0.0.1:61843/B4tZiYo4ANE=/ws
I/madra.brew_cre(10170): Background concurrent copying GC freed 28317(6MB) 
AllocSpace 
objects, 5(96KB) LOS objects, 49% free, 1944KB/3MB, paused 1.286ms total 114.385ms
D/eglCodecCommon(10170): setVertexArrayObject: set vao to 0 (0) 1 0
D/EGL_emulation(10170): eglMakeCurrent: 0xe9586440: ver 2 0 (tinfo 0xd0ce8ff0)

错误信息

Failed host lookup: '$host'

终端正常运行日志

Launching lib\main.dart on AOSP on IA Emulator in debug mode...
Running Gradle task 'assembleDebug'...                              9.7s
√  Built build\app\outputs\flutter-apk\app-debug.apk.
Syncing files to device AOSP on IA Emulator...                     217ms
A Dart VM Service on AOSP on IA Emulator is available at: 
http://127.0.0.1:62597/G44aJh3yYYE=/
The Flutter DevTools debugger and profiler on AOSP on IA Emulator is available at: 
http://127.0.0.1:9101?uri=http://127.0.0.1:62597/G44aJh3yYYE=/
D/eglCodecCommon(10659): setVertexArrayObject: set vao to 0 (0) 1 0
D/EGL_emulation(10659): eglMakeCurrent: 0xd67fd2e0: ver 2 0 (tinfo 0xe7b2ac70)

解决方法

  • 检查并重置VS Code调试配置
    打开项目根目录下的.vscode/launch.json,如果有手动指定port或vmServiceUri的配置,直接删除这些字段,让系统自动分配端口。示例配置如下:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Flutter",
          "request": "launch",
          "type": "dart"
        }
      ]
    }
    

    保存配置后重启VS Code再尝试调试。

  • 重置VS Code Flutter插件
    打开VS Code设置(快捷键Ctrl+,),搜索Flutter相关配置,将所有自定义的调试相关选项恢复默认;也可以卸载Flutter插件,清理VS Code缓存后重新安装插件。

  • 排查代理环境变量继承问题
    尽管系统设置了自动代理,VS Code调试控制台可能没正确继承终端的代理规则。可以尝试:

    1. 在VS Code内置终端先执行flutter run确保运行正常,再切换到调试控制台启动
    2. 在VS Code设置中添加代理排除列表,把127.0.0.1、localhost加入不使用代理的地址
  • 手动指定调试端口
    在.vscode/launch.json中添加终端运行时正常的端口参数,比如:

    {
      "name": "Flutter",
      "request": "launch",
      "type": "dart",
      "args": ["--debug-port", "62597"]
    }
    

    替换成你终端运行时显示的VM Service端口号,再启动调试。

  • 清理缓存并重启模拟器
    执行flutter clean清理项目缓存,再运行flutter pub get重新拉取依赖;同时关闭Android模拟器,等待几秒后重新启动,确保模拟器网络连接正常。

内容的提问来源于stack exchange,提问作者HungerMadra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 06:45:02