在 flutter 桌面应用程序上构建后无法复制文件

发布于 2025-01-16 22:18:18 字数 754 浏览 1 评论 0原文

在我的项目中,我可以按保存按钮在调试模式下复制 flutter 内的文件,并且一切正常(复制的文件),但是在构建并打开发布应用程序后,我无法复制文件而没有错误消息,只是按下按钮,什么也没发生。

我尝试以管理员身份运行发布应用程序,但还是同样的问题。

 Widget buildButton() {
final isFormValid = name.isNotEmpty && path.isNotEmpty;

return Padding(
  padding: EdgeInsets.symmetric(vertical: 8, horizontal: 12),
  child: ElevatedButton(
    style: ElevatedButton.styleFrom(
      onPrimary: Colors.white,
      primary: isFormValid ? null : Colors.grey.shade700,
    ),
    onPressed: () async {
      addOrUpdatePlan();
      File PDF= File('assets/PDF/file.pdf');
      File newPDF = await PDF.copy('$path/$name.pdf'); //////The Bug
    },
    child: Text(
      'SAVE',
      style: TextStyle(fontSize: 30),
    ),
  ),
);

}

in my project I can press the save button to copy a file inside flutter in debug mode and every thing works fine (the file copied), but after build and open the release application I can't copy the file with no error message, just the button pressed and nothing happened.

I tried to run the release application as administrator, but it's the same problem.

 Widget buildButton() {
final isFormValid = name.isNotEmpty && path.isNotEmpty;

return Padding(
  padding: EdgeInsets.symmetric(vertical: 8, horizontal: 12),
  child: ElevatedButton(
    style: ElevatedButton.styleFrom(
      onPrimary: Colors.white,
      primary: isFormValid ? null : Colors.grey.shade700,
    ),
    onPressed: () async {
      addOrUpdatePlan();
      File PDF= File('assets/PDF/file.pdf');
      File newPDF = await PDF.copy('$path/$name.pdf'); //////The Bug
    },
    child: Text(
      'SAVE',
      style: TextStyle(fontSize: 30),
    ),
  ),
);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

執念 2025-01-23 22:18:18

您在 File 构造函数中使用相对路径;这意味着仅当应用程序运行时的当前工作目录恰好是该路径相对于的目录时,它才会起作用。应用程序的工作目录不是由应用程序所在的位置控制的,而是由应用程序的启动方式控制的,因此不属于应用程序开发人员的控制范围。

编写应用程序时,应始终使用绝对路径,而不是相对路径。如何为您的用例构建绝对路径取决于预期的基本目录是什么。

You are using a relative path in your File constructor; that means that it will only work if the current working directory when the application is run happens to be the directory that the path is relative to. The working directory of an application is not controlled by where the application is, but how it was launched, so is not within your control as an application developer.

You should always use absolute paths, not relative paths, when writing applications. How you construct an absolute path for your use case depends on what the intended base directory is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文