使用Flutter Firebase Storage插件上传文件

4

我对Flutter和Firebase都是新手,所以请您原谅。我尝试使用Flutter Firebase存储插件上传文件到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存储插件示例中的代码相似,如下所示。只是异常在执行倒数第二行时抛出(也就是下面代码的最后一行永远不会被执行,因为异常已经被抛出)。

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个回答

2

使用

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

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

2

尝试:

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

这对我很有帮助。

2

对我来说完美运作:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接