Uploading File with Flutter Firebase Storage Plugin
Uploading File with Flutter Firebase Storage Plugin
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!
1 Answer
1
Use
final FirebaseStorage storageRef = FirebaseStorage.instance.ref().child('someCollection');
final StorageReference ref = storageRef.child(basename(file.path));
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.