Reputation: 698
I'm new to both Flutter and Firebase, so bear with me on this one. I get the following exception when trying to upload a file to Firebase Storage using the flutter firebase storage plugin
java.lang.IllegalArgumentException: The storage Uri cannot contain a path element.
With some more information below.
D/FirebaseApp(17988): com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
E/MethodChannel#plugins.flutter.io/firebase_storage(17988): Failed to handle method call
E/MethodChannel#plugins.flutter.io/firebase_storage(17988): java.lang.IllegalArgumentException: The storage Uri cannot contain a path element.
E/MethodChannel#plugins.flutter.io/firebase_storage(17988): at com.google.firebase.storage.FirebaseStorage.zza(Unknown Source:24)
E/MethodChannel#plugins.flutter.io/firebase_storage(17988): at com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source:37)
E/MethodChannel#plugins.flutter.io/firebase_storage(17988): at io.flutter.plugins.firebase.storage.FirebaseStoragePlugin.onMethodCall(FirebaseStoragePlugin.java:53)
E/MethodChannel#plugins.flutter.io/firebase_storage(17988): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:191)
E/MethodChannel#plugins.flutter.io/firebase_storage(17988): at io.flutter.view.FlutterNativeView.handlePlatformMessage(FlutterNativeView.java:163)
E/MethodChannel#plugins.flutter.io/firebase_storage(17988): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_storage(17988): at android.os.MessageQueue.next(MessageQueue.java:379)
E/MethodChannel#plugins.flutter.io/firebase_storage(17988): at android.os.Looper.loop(Looper.java:144)
E/MethodChannel#plugins.flutter.io/firebase_storage(17988): at android.app.ActivityThread.main(ActivityThread.java:7425)
E/MethodChannel#plugins.flutter.io/firebase_storage(17988): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_storage(17988): at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
E/MethodChannel#plugins.flutter.io/firebase_storage(17988): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
My code for uploading is similar to that in the examples of the flutter firebase storage plugin, and can be seen below. The exception is thrown during the execution of the second to last line (that is - the final line in the code below is never executed because of the exception being thrown).
final FirebaseStorage storage = new FirebaseStorage(
app: app, storageBucket: 'gs://myAppId.appspot.com/someCollection');
final StorageReference ref = storage.ref().child(basename(file.path));
var upload = ref.putFile(file);
final Uri downloadUri = (await upload.future).downloadUrl;
final downloadUrl = downloadUri.toString();
Any help appreciated!
Upvotes: 4
Views: 2334
Reputation: 111
Works perfectly for me:
void onUploaded(String type, String downloadUrl) {
/* todo on uploaded */
}
void onFailed(String type) {
/* todo on failed */
}
Future<dynamic> upload({String originalFile, Function onUploaded, Function onFailed, String type}) async {
File fileToUpload = new File(originalFile);
String renameFileTo = Date.getCurrentTimeStamp();
StorageReference reference = FirebaseStorage.instance.ref().child(renameFileTo);
StorageUploadTask uploadTask = reference.putFile(fileToUpload);
StorageTaskSnapshot storageTaskSnapshot = await uploadTask.onComplete;
storageTaskSnapshot.ref.getDownloadURL().then((downloadUrl) {
onUploaded(type, downloadUrl);
}).catchError(() {
onFailed(type);
});
/* todo check redundant files */
}
upload(originalFile: "/file/originalFile.png", onUploaded:onUploaded, onFailed:onFailed, type:"image");
Upvotes: 2
Reputation: 432
Try:
Future uploadFile(){
final String fileName = DateTime.now().toString();
StorageReference storageReference =
FirebaseStorage.instance.ref().child("images/$fileName");
StorageUploadTask uploadTask = storageReference.putFile(_image);
// await uploadTask.onComplete;
print('File Uploaded');
uploadedFileURL = await(await uploadTask.onComplete).ref.getDownloadURL();
print(uploadedFileURL);
}
This worked for me.
Upvotes: 2
Reputation: 657268
Use
final FirebaseStorage storageRef = FirebaseStorage.instance.ref().child('someCollection');
final StorageReference ref = storageRef.child(basename(file.path));
Upvotes: 2