使用vs code 创建第一个flutter web 项目hello_world

时间:2024-03-18 13:41:27

使用vs code的  Flutter: New Web Project  创建 hello_world项目,本来非常简单的一个过程,因为flutter web还没发布,也没完整文档,遇到了一些问题,记录在这里,希望对后来者有帮助。

  • run the Flutter: New Web Project command from VS Code("查看->命令面板" 或快捷键 Ctrl + Shift +P)

使用vs code 创建第一个flutter web 项目hello_world

使用vs code 创建第一个flutter web 项目hello_world

  • 创建项目后console输出卡住了

使用vs code 创建第一个flutter web 项目hello_world

【如果没有卡住,则忽略git url的修改,但是因为国内访问github慢,可以修改为访问本地下载的flutter_web】

使用vs code 创建第一个flutter web 项目hello_world

  • 修改pubspec.yaml,其实就是flutter web的git 配置错了,增加.get 就ok

修改前:

使用vs code 创建第一个flutter web 项目hello_world

修改后:

使用vs code 创建第一个flutter web 项目hello_world

 

  • 取消先前 pub get操作,重新执行命令(ctrl + shift +p),

使用vs code 创建第一个flutter web 项目hello_world

使用vs code 创建第一个flutter web 项目hello_world

如果pub get报错,可以先尝试 pub upgrade

  • github 太慢,可以直接使用本地下载的flutter-web

这个最大的问题是github在国内访问太慢,几乎不能使用(偶尔网络好点勉强可用)。

解决github慢的问题主要是修改hosts,解决dns解析问题。

192.30.253.120 codeload.github.com
192.30.253.112 github.com
192.30.253.113 github.com
151.101.185.194 github.global.ssl.fastly.net
185.199.108.153 assets-cdn.github.com
185.199.109.153 assets-cdn.github.com
185.199.110.153 assets-cdn.github.com
185.199.111.153 assets-cdn.github.com
52.216.137.246 s3.amazonaws.com 
52.216.237.147 github-cloud.s3.amazonaws.com

  • 终于可以执行了(F5或debug-> Start Debugging).

使用vs code 创建第一个flutter web 项目hello_world

  • 测试热更新(hot reload)

修改lib/main.dart 代码

使用vs code 创建第一个flutter web 项目hello_world

保存后,重新刷新浏览器(不需要重新运行),即可看到修改后的结果

使用vs code 创建第一个flutter web 项目hello_world