【问题标题】:Flutter: Upload Image Unhandled Exception: PlatformException(firebase_storage, The storage Uri cannot contain a path element., {}, null)Flutter: Upload Image Unhandled Exception: PlatformException(firebase_storage, The storage Uri cannot contain a path element., {}, null)
【发布时间】:2021-07-05 14:58:50
【问题描述】:

我想知道为什么我在尝试上传图片时收到此错误。

class StorageRepository {
  FirebaseStorage storage = FirebaseStorage.instanceFor(
    bucket:
        '', (Edit: The link was only wrong)
  );

  Future<String> uploadFile(File file) async {
    var userUID = getCurrentUsersUID();
    var storageRef = storage.ref().child("user/profile/$userUID");
    UploadTask uploadTask = storageRef.putFile(file);
    String downloadUrl = await (await uploadTask).ref.getDownloadURL();
    userInstance.avatarUrl = downloadUrl;

    return downloadUrl;
  }
}

这是我的方法:

onPressed: () async {
   var image = await ImagePicker().getImage(source: ImageSource.gallery);
   var imageFile = File(image.path);
   StorageRepository().uploadFile(imageFile);
   setState(() {
    });
   },

错误信息:

E/flutter (20967): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(firebase_storage, The storage Uri cannot contain a path element., {}, null)

【问题讨论】:

    标签: firebase flutter google-cloud-firestore firebase-storage


    【解决方案1】:

    来自documentation

     FirebaseStorage storage =  FirebaseStorage.instanceFor(
          bucket: 'secondary-storage-bucket');
    

    您需要获取存储桶的名称,但您将项目的url\path 传递给它。转到您的项目并获取您要使用的存储的存储桶名称。这一切都假设您想使用除主 firebaseApp 之外的另一个存储桶。否则,只需使用:

    Future<String> fireBaseUploadFile(String filePath) async {
    FirebaseStorage storage = FirebaseStorage.instance;
       Reference ref = FirebaseStorage.instance.ref('/productImages/').child('someimageID');
      File file = File(filePath);
    
      try {
        await ref.putFile(file);
      } on FirebaseException catch (e) {
        print(e);
      }
      return ref.getDownloadURL();
    }
    

    让生活更轻松。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-16
      • 2020-12-30
      • 1970-01-01
      • 2021-09-06
      • 2020-11-16
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多