Windows+Nginx 部署 Flutter Web

本文介绍了如何使用Flutter SDK 2.x创建支持Web的项目,通过`flutter create`命令添加Web支持,然后使用`flutter build web`进行编译。编译完成后,通过Nginx配置将Flutter Web应用部署在本地服务器,并提供了Nginx配置示例。只需访问http://localhost:8083/index.html即可查看Web应用。

今天的内容比较简单,我们直接进入主题。


创建工程

目前笔者用的 Flutter SDK 是 Stable 2.x,其已经支持 Web了。

使用 Android Studio 创建 Flutter 工程,需要勾选上对 Web 的支持,如下图所示:
在这里插入图片描述

如果你之前创建的工程没有勾选该选项,可以在工程目录下执行如下命令:

# flutter create <output directory>
# Create a new Flutter project in the specified directory.
flutter create .
Recreating project ....
  web\favicon.png (created)
  web\icons\Icon-192.png (created)
  web\icons\Icon-512.png (created)
  web\index.html (created)
  web\manifest.json (created)

Wrote 8 files.

All done!

完成之后,在工程目录下就会生成 web 目录。
在这里插入图片描述

编译 Web 版本

编译 Web 版本只需要一行命令

flutter build web

编译成功后,在工程的 build 目录下面有个 web 目录。

Nginx 配置

在 Nginx 的配置文件中,增加一个 server 配置即可。

server {
        listen       8083;
        server_name  localhost;
        location / {
            # web目录的路径
            root   \project-path\build\web;
            # 默认页面
            index  index.html index.htm;
        }
    }

启动 Nginx(关于 Windows 上面操作 Nginx 的命令,可以参考笔者的 减少跨域中的OPTIONS请求) ,然后在浏览器中访问 http://localhost:8083/index.html 即可。


学习犹如爬山,适当的咬紧牙关冲刺一把,就能一览众山小
在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值