flutter web之WebSocketChannelException错误

flutter最近新功能增加挺多,试了FFI后,尝试下flutter web,但是执行flutter run -d chrome时却报如下错误:

~/w/f/flutter_web_demo ❯❯❯ flutter run -d chrome                                                                   ✘ 1
Launching lib/main.dart on Chrome in debug mode...
Building application for the web...    
⣻WebSocketChannelException: WebSocketChannelException: 
SocketException: 
Failed host lookup: 'localhost' (OS Error: nodename nor servname provided, or not known, errno = 8)
       ⣻^C

报错报的莫名其妙。。。原来没发布时用webdev尝试flutter_web的工程也没有问题啊

网上搜了下,都是一些和我这个完全不搭的问题,只能自己尝试解决了。

仔细看了下报错信息:

Failed host lookup: 'localhost' (OS Error: nodename nor servname provided, or not known

localhost找不到,为什么呢?

忽然想到了今天安装了SwitchHosts!,然后修改了/etc/hosts。打开/etc/hosts看下内容,果然一篇空白,什么都没有。使用SwitchHosts!还原默认hosts文件,也就是增加如下内容到hosts:

127.0.0.1	localhost

重新运行flutter run -d chrome,运行成功,自动打开chrome,完美。

正常输出:

~/w/f/flutter_web_demo ❯❯❯ flutter run -d chrome                                                                              ✘ 130
Launching lib/main.dart on Chrome in debug mode...
Building application for the web...                                21.3s
Attempting to connect to browser instance..                         6.4s

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
Warning: Flutter's support for building web applications is highly experimental.
For more information see [https://github.com/flutter/flutter/issues/34082.](https://github.com/flutter/flutter/issues/34082.)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

?  To hot restart (and rebuild state), press "R".
For a more detailed help message, press "h". To quit, press "q".
Debug service listening on ws://127.0.0.1:57994/Bt0EHzyriqw=

我原创发于:https://cloud.tencent.com/developer/article/1517005

你可能感兴趣的:(flutter,flutter,web)