我试图使用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);
}
}
1条答案
按热度按时间tkclm6bt1#
请再次检查仿真器路径。
你也跟着这个问题
Flutter - save file to download folder - downloads_path_provider