是否可以在本地环境中使用基于Flutter 桌面的应用程序托管Flutter Web应用程序?

推荐答案

谷歌搜索这样的解决方案可能很困难,因为它涉及许多导致类似情况的关键字(当您需要本地解决方案时,在线托管,仅命令行解决方案,等等).

经过一番挖掘,我最终使用shelf包在本地网络上部署了我自己的Ffltter Web应用程序.这是我为Windows开发的,所以我不能保证它能在其他平台上运行. 显然,首先要做的是将shelf包添加到pubspec.yaml中:之后,我的main方法如下所示

import 'package:shelf/shelf_io.dart' as shelf_io;  
import 'package:shelf/shelf.dart' as shelf;  
import 'package:shelf_router/shelf_router.dart' as shelf_router;  

[...]  

void main() async{  

[...]    

  var secureContext = SecurityContext();  
  try {  
    //privKey and cert are the String names of the two files for the SSL connection,  
    //placed in the root directory of the flutter project or along with the .exe  file (when released)
    secureContext.usePrivateKey(privKey);  
    secureContext.useCertificateChain(cert);  
  } catch (error) {  
    logger.e("Error on init SecurityContext");  
  }
  try {  
    //this is the handler that deploys the files contained in 'webAppFolder': I just simply pasted the result of  
    //the flutter webapp building inside (the index.html file is the default one for flutter web)   
    //and put the folder in the root of the flutter project (or, again, in the same folder with the .exe file when released)
    final _staticHandler = createStaticHandler("webAppFolder", defaultDocument: 'index.html');    

    //this I kept just for a reminder on how to deploy a static page, if needed
    final _router = shelf_router.Router()  
      ..get(  
        '/time',
        (request) => shelf.Response.ok(DateTime.now().toUtc().toIso8601String()),  
      );  
  
    final cascade = shelf.Cascade()   
        .add(_staticHandler)  
        .add(_router);  
  
    try {  
      var server = await shelf_io.serve(  
        cascade.handler,  
        InternetAddress.anyIPv4,  
        mainPort,  //this is the number of the port on which the webapp is deployed (I load this from a .ini file beforehand
        securityContext: secureContext,  
      );  
      // Enable content compression  
      server.autoCompress = true;  
  
      logger.i("Serving at https://${server.address.host}:${server.port}");  
    } catch (err) {  
      logger.e("Error while serving");  
      logger.e(err.toString());  
    }  
  } catch (err) {  
    logger.e("Error while creating handler");  
    logger.e(err.toString());  
  }  
  runApp(MaterialApp(  
  [...]

This is the part related to the deploy of a web app: since the flutter desktop app already provides a GUI, I used that to add some maintenance and testing utilities to check if everything is working fine.
For more details regarding shelf, refer to their API on their pub.dev page.

Flutter相关问答推荐

关闭视频通话,相机仍在运行!(Flutter WebRTC)

如何等待抖动S定时器.定期取消?

将目标平台设置为Flutter 构建

无法在我的Flutter 应用程序中使用GoRouter测试导航

如何删除使用isscllable时出现的左边距?

Flutter 需要 Xcode 14 或更高版本

flutter Listview构建器溢出

显示图像的放大部分

添加appBar时绘制的线条发生偏移 Flu

允许文本小部件在容器之间溢出

splash_screen 没有被推送到登录页面

Flutter ImagePicker - 在 onPressed 中异步获取图像显示 lint 警告

如何将此文本按钮剪辑在堆栈中的圆形头像上方?

Freezed+BLoc 如何从 BLoc Listener 查看当前状态和特定状态的变量

从子类调用超类的超方法

如何在两个不同的路由(屏幕)之间正确设置 BlocProvider?

Flutter Web App 未启动:脚本的 MIME 类型不受支持

如何将图像网络装箱到具有边界半径的容器中

带图像封面的 ElevatedButton

Flutter 广告导致延迟