downloadFile method
Downloads a file from Firebase Storage at firestorePath
.
The file can be saved under the optional name fileName
.
Implementation
Future<void> downloadFile(
{required String firestorePath, String fileName = ''}) async {
if (Platform.isIOS || Platform.isAndroid) {
bool status = await Permission.storage.isGranted;
if (!status) {
await Permission.storage.request();
}
}
Directory appDocDir = await getTemporaryDirectory();
File downloadToFile = File('${appDocDir.path}/${basename(firestorePath)}');
try {
await storage.ref(firestorePath).writeToFile(downloadToFile);
await FilePicker.platform.saveFile(
fileName:
fileName.isNotEmpty ? fileName : basename(downloadToFile.path),
bytes: downloadToFile.readAsBytesSync());
} catch (e) {
print(e);
}
}