can't reupload picture to Firebase Storage in Flutter

34 Views Asked by At

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();
});
1

There are 1 best solutions below

0
mrKaraaslan On

Your app fails to create the var file because it has subfolders. You should create it like:

var file = await File("${appDocDir.absolute}/images/temp.jpg").create(recursive: true);