ios 在flutter & dart中写入文件时出错

vsaztqbk  于 2022-11-19  发布在  iOS
关注(0)|答案(1)|浏览(315)

我试图使用Flutter从服务器下载一个文件。它工作得很好,但我从Android模拟器卸载了应用程序,现在它没有请求权限,并生成以下错误。

Unhandled Exception: FileSystemException: Cannot create file, path = '/storage/emulated/0/Download/Salary-Sheet.xlsx'

代码

Future<bool> getStoragePermission() async {
    return await Permission.storage.request().isGranted;
  }

  Future<String> getDownloadPath() async {
    return await ExternalPath.getExternalStoragePublicDirectory(
        ExternalPath.DIRECTORY_DOWNLOADS);
  }

  downloaddd(String downloadPath) async {
    var path = '$downloadPath/Salary-Sheet.xlsx';
    String url =
        "http://salary/export/${widget.masterID}";

    await dio.download(url, path, onReceiveProgress: (received, total) {
      if (total != -1) {
        Navigator.pop(context);
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            behavior: SnackBarBehavior.floating,
            content: Row(
              children: const [
                Icon(Icons.cloud_download),
                SizedBox(
                  width: 10,
                ),
                Text("Salary sheet has been downloaded !"),
              ],
            ),
          ),
        );
        //you can build progressbar feature too
      }
    });
  }

  doDownloadFile() async {
    if (await getStoragePermission()) {
      String downloadDirectory = await getDownloadPath();
      await downloaddd(downloadDirectory);
    }
  }

相关问题