I am trying to download an image from Firebase Storage in Flutter and upload it again under a different path. I try to download the image, save it to a file and then upload it to the new path. but I get the following error:
Unhandled Exception: 'package:firebase_storage/src/reference.dart': Failed assertion: line 127 pos 12: 'file.absolute.existsSync()': is not true.
The code I am using is:
final appDocDir = await getApplicationDocumentsDirectory();
var file = File("${appDocDir.absolute}/images/temp.jpg");
final downloadPath = FirebaseStorage.instance.ref().child(foreignPath);
final downloadTask = downloadPath.writeToFile(file);
await downloadTask.whenComplete(() async {
var ref = FirebaseStorage.instance.ref().child(path);
task = ref.putFile(file); //<-- error here
final snapshot = await task?.whenComplete(() {});
final urlDownload = await snapshot?.ref.getDownloadURL();
});
Your app fails to create the
var filebecause it has subfolders. You should create it like: