使用Flutter Firebase存储插件上传文件

时间:2018-08-19 19:18:42

标签: firebase flutter firebase-storage

我对Flutter和Firebase还是陌生的,所以请耐心等待。尝试使用flutter firebase storage plugin

将文件上传到Firebase存储时,出现以下异常
java.lang.IllegalArgumentException: The storage Uri cannot contain a path element.

下面有更多信息。

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)

我的上传代码与flutter firebase storage plugin的示例中的代码相似,可以在下面看到。在执行倒数第二行的过程中会引发异常(也就是说,由于抛出了异常,因此以下代码中的最后一行永远不会执行)。

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

任何帮助表示赞赏!

3 个答案:

答案 0 :(得分:2)

对我来说很完美:

LayoutBuilder(
    builder: (BuildContext context, BoxConstraints constraints) {
        // use parent's constraint here
        return ChildWidget();
    }
)

答案 1 :(得分:1)

使用

final FirebaseStorage storageRef = FirebaseStorage.instance.ref().child('someCollection');

final StorageReference ref = storageRef.child(basename(file.path));

答案 2 :(得分:0)

尝试:

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

这对我有用。