flutter web之WebSocketChannelException錯誤

  • 2019 年 10 月 11 日
  • 筆記

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=