【问题标题】:Flutter - delete directory failed (OS Error: Operation not permitted, errno = 1)Flutter - 删除目录失败(操作系统错误:不允许操作,errno = 1)
【发布时间】:2022-01-05 14:02:59
【问题描述】:

我有一个颤振应用程序,将 pdf 文件保存在临时目录中,然后用户可以按一个按钮删除这些文件,在 Android 中一切正常,但在 iOS 中我收到此错误:

未处理的异常:FileSystemException:删除失败,路径 = '/var/mobile/Containers/Data/Application/75048CC8-24E7-4BA3-B748-5603D4F74F60/Library/Caches' (操作系统错误:不允许操作,errno = 1)

奇怪的是,如果您刷新应用程序,您会看到文件已被删除,因此实际上它可以工作,但会出错并阻止应用程序。

我的删除代码是这样的:

CupertinoDialogAction(
    child: Text("Ok"),
    onPressed: () async {
        Directory dir = await getTemporaryDirectory();
        dir.deleteSync(recursive: true);
        dir.create();
        Navigator.pop(context);
        openFinishDialog();
    }
),

此外,此错误仅显示在物理设备中,而不显示在模拟器中。

我使用的是 dart 包 path_provider 2.0.8

【问题讨论】:

    标签: ios flutter


    【解决方案1】:

    好的,我通过在 TemporaryDirectory 中创建一个目录并删除它而不是 TemporaryDirectory 解决了这个问题,这里是创建代码:

    var tempDir = await getTemporaryDirectory();
    var tempDirPath = tempDir.path;
    final myAppPath = '$tempDirPath/my_app';
    final res = await Directory(myAppPath).create(recursive: true);
    

    这是删除的代码:

    Directory dir = await getTemporaryDirectory();
    Directory myDir = Directory(dir.path + "/my_app");
    myDir.deleteSync(recursive: true);
    myDir.create();
    

    我希望这对其他人有用,如果有人有更好的方法来解决这个问题,请回答!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 2021-04-04
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 2012-08-18
      相关资源
      最近更新 更多