Flutter创建MSIX安装包时遭遇未处理异常求助
从报错日志来看,核心问题是msix工具尝试加载证书文件时找不到指定路径,大概率是路径格式冲突(混合了正斜杠/和反斜杠\)或自定义证书配置未生效导致工具仍在调用默认测试证书。以下是具体修复步骤:
确认自定义证书配置生效
打开项目根目录的pubspec.yaml,检查msix配置块是否正确指定了你的自签名证书路径和密码,路径统一使用正斜杠/或转义反斜杠\\,示例:msix_config: certificate_path: C:/Users/your-name/Documents/self-signed-cert.pfx certificate_password: your-cert-password display_name: YourAppName publisher_display_name: YourPublisherName清理缓存并重新构建
- 清理Flutter构建产物:
flutter clean - 重新构建Windows项目:
flutter build windows - 清理pub缓存并重装msix依赖:
dart pub cache clean,再执行dart pub add msix - 重新运行MSIX创建命令:
dart run msix:create
- 清理Flutter构建产物:
验证证书路径有效性
把报错中的证书路径修正为统一斜杠格式,比如将C:/Users/user/AppData/Local/Pub/Cache/hosted/pub.dev/msix-3.16.1/lib/assets\test_certificate.pfx改为C:\Users\user\AppData\Local\Pub\Cache\hosted\pub.dev\msix-3.16.1\lib\assets\test_certificate.pfx,打开文件管理器确认文件是否存在。若不存在,说明msix包安装不完整,需重新安装。手动测试证书加载
打开PowerShell,执行以下命令(替换为你的证书路径和密码)验证证书是否可用:new-object System.Security.Cryptography.X509Certificates.X509Certificate2("C:/path/to/your/cert.pfx", "your-password") | select -expandproperty Subject -First 1若命令报错,说明证书损坏、密码错误或路径无效,需重新生成自签名证书。
检查PowerShell执行权限
执行Get-ExecutionPolicy查看权限设置,若不是RemoteSigned或Unrestricted,以管理员身份运行PowerShell并执行Set-ExecutionPolicy RemoteSigned,确保脚本可正常执行。
内容的提问来源于stack exchange,提问作者RBamba

