Here is my code:
Future selectFile() async {
final result =
await ImagePicker.platform.pickImage(source: ImageSource.gallery);
if (result == null) return;
final path = result.path;
setState(() {
file = File(path);
});
}
Future uploadFile() async {
if (file == null) return;
final fileName = file!.path.split("/").last;
final destination = "test/";
Storage.uploadFile(destination, file!);
}
import 'dart:io';
import 'package:firebase_storage/firebase_storage.dart';
class Storage {
static Future<void> uploadFile(String destination, File file) async {
try {
final ref = FirebaseStorage.instance.ref().child(destination);
await ref.putFile(file);
} on FirebaseException catch (e) {
print(e);
}
}
}
I can’t seem to work out why exactly the code does not upload a photo, I have changed the rules in firebase but to no avail and the folder is called test so if anyone could suggest what I do or how to test my firebase storage, that would be a great help. Thanks in advance.
I keep getting this error when I call uploadFile:
E/StorageException(27972): at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:272)
E/StorageException(27972): at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:289)
E/StorageException(27972): at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:76)
E/StorageException(27972): at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:68)
E/StorageException(27972): at com.google.firebase.storage.UploadTask.sendWithRetry(UploadTask.java:477)
E/StorageException(27972): at com.google.firebase.storage.UploadTask.beginResumableUpload(UploadTask.java:276)
E/StorageException(27972): at com.google.firebase.storage.UploadTask.run(UploadTask.java:224)
E/StorageException(27972): ... 5 more
I/flutter (27972): [firebase_storage/object-not-found] No object exists at the desired reference.
Firebase rules are:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if true;
}
}
}
UPDATE
I created a new flutter application with the above code and just two buttons, so it seems it may be a flaw in my other application’s dependencies or something like that rather than the code. Thank you to everyone who helped once I figure out how to get it to work on my original application I will update.
3
Answers
I have the similar problem and it solve like this.
The reference you created should look like this now you added ref to the destination. please try the following
EDIT
Edit1*
Remove static from here.
When you wish to upload first make a reference
This should work as expected
final firebaseStorage = FirebaseStorage.instance.ref();
// late var user = _auth.currentUser;
var postid = Uuid().v1();
Future postData( imageFile) async {
"mediaurl": Iurl,
}