小工具知识网

当前位置: 首页 > 行业知识 > Flutter使用auto

行业知识

Flutter使用auto

2026-11-09 05:42:52 来源:互联网转载

因为windows应用一般大家都是从网上下载的,后期版本肯定会更新,那用flutter开发windows应用,怎么实现应用内版本更新功能了?可以使用auto_updater库,

这个插件允许 Flutter 桌面 应用自动更新自己 (基于 sparkle 和 winsparkle)

地址如下:

pub仓库地址:auto_updater | Flutter Package

github仓库地址:https://github.com/leanflutter/auto_updater

中文文档地址:https://github.com/leanflutter/auto_updater/blob/main/README-ZH.md

开始使用

先添加依赖,将此添加到你的软件包的 pubspec.yaml 文件:

dependencies:  auto_updater: ^0.1.7

windows使用需要安装openssl:

安装教程:windows安装Chocolatey方法注意事项,以及安装openssl方法-CSDN博客 

然后在main.dart中添加下面代码:

import 'package:auto_updater/auto_updater.dart';void main() async {  // 必须加上这一行。  WidgetsFlutterBinding.ensureInitialized();  String feedURL = 'http://localhost:5002/appcast.xml';  await autoUpdater.setFeedURL(feedURL);  await autoUpdater.checkForUpdates();  await autoUpdater.setScheduledCheckInterval(3600);  runApp(MyApp());}

生成私钥

运行以下命令:需要分别在 macOS 和 Windows 系统中运行该命令。

dart run auto_updater:generate_keys

命令将为你生成私钥(dsa_priv.pem)及公钥(dsa_pub.pem),请备份你的私钥并确保其安全,并将公钥作为 Windows 资源添加到项目中。 

macOS运行命令后输出:

A key has been generated and saved in your keychain. Add the `SUPublicEDKey` key tothe Info.plist of each app for which you intend to use Sparkle for distributingupdates. It should appear like this:    SUPublicEDKey    pfIShU4dEXqPd5ObYNfDBiQWcXozk7estwzTnF9BamQ=

更改文件 macos/Runner/Info.plist 如下: 

  ...+SUPublicEDKey+bHaXClrRGMmKoKP/3HJnr/jn2ODTRPAM3VZhhkI9ZvY=

Windows运行命令后输出:

Generated two files:dsa_priv.pem: your private key. Keep it secret and don't share it!dsa_pub.pem: public counterpart to include in youe app.BACK UP YOUR PRIVATE KEY AND KEEP IT SAFE!If you lose it, your users will be unable to upgrade!

更改文件 windows/runner/Runner.rc 如下:

...+/+//+// WinSparkle+//+// And verify signature using DSA public key:+DSAPub      DSAPEM      "../../dsa_pub.pem"

打包应用

为了简化打包的过程,这里使用了 Flutter Distributor ,一个专门用于打包和发布 Flutter 应用的完整工具。官方网站:Getting Started | Flutter Distributor

安装flutter_distributor:

#安装命令dart pub global activate flutter_distributor

安装Inno setup

必须安装Inno Setup 6 并且添加中文语言包,且安装目录要使用默认的C盘里面,否则打包失败。因为Flutter Distributor打包exe是基于inno setup 6实现的,默认官网是英文版本的,打包exe之后点击安装都是英文,所以我找到了中文版的inno setup 6和中文语言包,下载地址如下

Inno Setup 6和中文安装包下载地址:inno setup 带中文包.zip - 蓝奏云

添加distribute_options.yaml添加到你的项目根目录 

output: dist/releases:  - name: dev    jobs:      - name: release-windows        package:          platform: windows          target: exe          build_args:            dart-define:              APP_ENV: dev

添加 make_config.yaml 到你的项目 windows/packaging/exe (需要手动创建)目录:

make_config.yaml内容: (安装目录和快捷方式的名字,发布者名称配置等)

# AppId 的值唯一标识此应用。# 不要在其他应用的安装程序中使用相同的 AppId 值。# 以下为示例代码,实际根据你的项目进行替换app_id: 5B566538-42B1-4826-A479-AF079F24A65Dpublisher: Jory Caidisplay_name: Hello Hadocreate_desktop_icon: trueinstall_dir_name: hado_worldlocales:  - en  - zh

开始生成exe文件:

flutter_distributor package --platform windows --targets exe

编译成功后:

对生成的exe文件进行升级签名:后面的路径要是你自己生成的文件路径

dart run auto_updater:sign_update dist/1.1.1/flutter_windows-1.1.1+1.1.1-windows-setup.exe

签名后的输入要添加到appcast.xml后面的dsaSignature属性里面:

appcast.xml内容:(配置更新版本和更新说明,还有更新文件下载链接等内容)

            auto_updater_example        Most recent updates to auto_updater_example        en                    Version 1.1.0            #发行说明-读取html方式(2选1)                            https://your_domain/your_path/release_notes.html                        #发行说明-写***方式(2选1)                                          
  • 1、新增XX功能
  • 2、新增XX功能
  • ]]> Sun, 16 Feb 2022 12:00:00 +0800 #你更新程序的地址 Version 1.1.0 #发行说明-读取html方式(2选1) https://your_domain/your_path/release_notes.html #发行说明-写***方式(2选1)
  • 1、新增XX功能
  • 2、新增XX功能
  • ]]> Sun, 16 Feb 2022 12:00:00 +0800 #你更新程序的地址

    新版本更新操作

    如果你的程序后续更新了,版本升级了,就修改项目根目录里面的 pubspec.yaml文件版本:

    修改完之后,再执行命令重新打包: 

    flutter_distributor package --platform windows --targets exe

    打包后就会在项目根目录dist下面出现响应版本的exe:

    然后运行命令对这个exe进行签名: 

    dart run auto_updater:sign_update dist/1.1.0/flutter_windows-1.1.0+1.1.0-windows-setup.exe

    将签名后的结果添加到appcast.xml里面:

    并且修改appcast.xml里面响应的版本和更新说明:

    然后启动历史版本的程序,检查更新:

    点击安装更新后:

    等待下载完成:点击安装更新

    就会杀***现在的进程,然后重新执行安装操作:

    上一篇:服务器名称怎么调出来?

    下一篇:服务器怎么打造的啊?