我构建了一个应用程序来将附件上传到 Firebase 存储,并将 URL 保存在 Firestore 中。上传部分效果很好。我还添加了一个下载按钮,用于下载相关附件。单击下载按钮时,应用程序应拆分最后一个“。”识别文件类型并下载相关文件路径。然而,不幸的是,当点击下载按钮时,出现了错误。
UI 截图
文件 firestore url 的示例
https://firebasestorage.googleapis.com/v0/b/petpulz-93d6f.appspot.com/o/records%2F49785b2d-5bc2-47c1-9ac2-2654627851de3518522212366212285.jpg?alt=media&token=4690a174-c094-4019-a540-17ca8437a69e.jpg
我的代码
IconButton(
icon: Icon(Icons.download),
onPressed: () async {
// Get the file from _selectedFiles
final file = _selectedFiles[index];
// Open the download dialog
final directory = await getExternalStorageDirectory();
final savePath = path.join(directory!.path, 'Download', file.path.split('/').last);
await showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Download'),
content: Text('Do you want to download ${file.path.split('/').last}'),
actions: [
TextButton(
child: Text('Cancel'),
onPressed: () => Navigator.pop(context),
),
TextButton(
child: Text('Download'),
onPressed: () async {
// Download the file
await http.get(Uri.parse(file.path)).then((response) async {
final bytes = response.bodyBytes;
await File(savePath).writeAsBytes(bytes);
});
// Close the dialog
Navigator.pop(context);
// Show a message to indicate that the download is complete
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Download complete')),
);
},
),
],
),
);
},
),
路径提供程序包
path_provider: ^2.0.2
如何解决这个错误?