my code
Future<String> uploadFile(File _image) async {
int uploadTimestamp = DateTime.now().millisecondsSinceEpoch;
Reference ref = FirebaseStorage.instance.ref().child('posts/$uploadTimestamp');
UploadTask uploadTask = ref.putFile(_image);
final TaskSnapshot taskSnapshot = await uploadTask.whenComplete(() {});
final url = await taskSnapshot.ref.getDownloadURL();
return url;
}
- firebase_storage: ^10.3.1
- firebase_core: ^1.19.1
Error: [firebase_storage/object-not-found] No object exists at the desired reference.
CodePudding user response:
what are you trying to achieve. one suspeect here is youre not calling _image anywhere. Seem like youre trying to upload an int.
CodePudding user response:
You are not calling the
await ref.putFile(_image);
before the:
final TaskSnapshot taskSnapshot = await uploadTask.whenComplete(() {});
And then call:
await ref.getDownloadURL();
after the upload in order to get the download url.
and also try to listen to the different upload states like:
uploadTask.snapshotEvents.listen((TaskSnapshot taskSnapshot) {
switch (taskSnapshot.state) {
case TaskState.running:
final progress =
100.0 * (taskSnapshot.bytesTransferred / taskSnapshot.totalBytes);
print("Upload is $progress% complete.");
break;
case TaskState.paused:
print("Upload is paused.");
break;
case TaskState.canceled:
print("Upload was canceled");
break;
case TaskState.error:
// Handle unsuccessful uploads
break;
case TaskState.success:
// Handle successful uploads on complete
// ...
break;
}
});
CodePudding user response:
This could be useful. I put in together this.
Future<String> uploadImageToDefaultBucket(File image) async {
Reference firebaseStorageRef = FirebaseStorage.instance.ref("your-ref").child("your-path");
UploadTask uploadTask = firebaseStorageRef.putFile(image);
TaskSnapshot taskSnapshot = await uploadTask;
return taskSnapshot.ref.getDownloadURL();
}