firebase_storage object-not-found 在所需的参考颤动处不存在对象

Ahm*_*min 11 dart firebase flutter flutter-dependencies

重要提示:我发现了同样的问题,但它因调试信息不​​完整而关闭。

我将图像上传到 firebase 存储,然后获取该图像的下载 URL 并将其存储到 firebase,以便我可以使用该 URL 通过网络图像显示用户的个人资料图像。

之前我存储图像时工作正常

Reference storageRef = FirebaseStorage.instance.ref('images');
      file = await _compressImage(file: file,);
      await storageRef.putFile(file);
      final String downloadUrl = await storageRef.child(id).getDownloadURL();
      return downloadUrl;

Run Code Online (Sandbox Code Playgroud)

但是当我将图像存储在特定文件夹中之后

Reference storageRef = FirebaseStorage.instance.ref('images');
      file = await _compressImage(file: file, id: id);
      await storageRef
          .child(Get.find<UserController>().user.username)
          .child(id)
          .putFile(file);
      final String downloadUrl = await storageRef.child(id).getDownloadURL();
      return downloadUrl;

Run Code Online (Sandbox Code Playgroud)

它显示此错误。

 [firebase_storage/object-not-found] No object exists at the desired reference.
Run Code Online (Sandbox Code Playgroud)

这是可解释的代码: 我将可下载的 URL 存储在 newImage 变量中

 String newImage;
      if (_controller.file != null) {
        newImage = await Database().uploadFile(
            file: _controller.file,
            id: Get.find<UserController>().user.username);
        print("new image: " + newImage.toString());
      }
Run Code Online (Sandbox Code Playgroud)

但在这里,当我打印 newImage 的值时,它正在将null打印到控制台。

new image: null
Run Code Online (Sandbox Code Playgroud)

这是将图像上传到 firebase 存储的第二种方法。

Future<String> uploadFile({@required File file, @required String id}) async {
    try {
      file = await _compressImage(file: file, id: id);
      await storageRef
          .child(Get.find<UserController>().user.username)
          .child(id)
          .putFile(file);
      final String downloadUrl = await storageRef.child(id).getDownloadURL();
      return downloadUrl;
    } catch (e) {
      print(e);
    }
  }
Run Code Online (Sandbox Code Playgroud)

调试控制台:

E/StorageException(11376): StorageException has occurred.
E/StorageException(11376): Object does not exist at location.
E/StorageException(11376):  Code: -13010 HttpResult: 404
E/StorageException(11376): {  "error": {    "code": 404,    "message": "Not Found.  Could not get object",    "status": "GET_OBJECT"  }}
E/StorageException(11376): java.io.IOException: {  "error": {    "code": 404,    "message": "Not Found.  Could not get object",    "status": "GET_OBJECT"  }}
E/StorageException(11376):  at com.google.firebase.storage.network.NetworkRequest.parseResponse(NetworkRequest.java:434)
E/StorageException(11376):  at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(NetworkRequest.java:451)
E/StorageException(11376):  at com.google.firebase.storage.network.NetworkRequest.processResponseStream(NetworkRequest.java:442)
E/StorageException(11376):  at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:272)
E/StorageException(11376):  at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:286)
E/StorageException(11376):  at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:70)
E/StorageException(11376):  at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:62)
E/StorageException(11376):  at com.google.firebase.storage.GetDownloadUrlTask.run(GetDownloadUrlTask.java:76)
E/StorageException(11376):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
E/StorageException(11376):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
E/StorageException(11376):  at java.lang.Thread.run(Thread.java:764)
I/flutter (11376): [firebase_storage/object-not-found] No object exists at the desired reference.
I/flutter (11376): new image: null
Run Code Online (Sandbox Code Playgroud)

但是当我检查 firebase 存储并且图像已成功上传到那里时。

我所知道的是,图像已成功上传到 firebase 存储,但上述方法在上传图像之前以某种方式返回可下载的 URL。

小智 6

对我有用的东西有点出乎我的意料。首先,我有这样的代码,这是有多少示例有它的。

  Reference reference = storage.ref(filePath);

  UploadTask uploadTask = reference.putFile(imageToUpload);

  final storageSnapshot = uploadTask.snapshot;

  final downloadUrl = await storageSnapshot.ref.getDownloadURL();
Run Code Online (Sandbox Code Playgroud)

然后,我决定尝试一下。我注意到,如果我等待 putFile 调用,它会更改返回类型,即使它不是未来。

  Reference reference = storage.ref(filePath);

  final TaskSnapshot snapshot = await reference.putFile(imageToUpload);

  final downloadUrl = await snapshot.ref.getDownloadURL();
Run Code Online (Sandbox Code Playgroud)

你知道吗,这有效!很奇怪,考虑到您可以等待 putFile 调用并不明显。


小智 1

我因与您和 IDK 相同的问题而浪费了很多时间,但它可以通过在使用之前创建引用来工作:

    final FirebaseStorage feedStorage =
              FirebaseStorage.instanceFor(bucket: F.feedBucket);
    
          Reference refFeedBucket = feedStorage
              .ref()
              .child('venues')
              .child(auth.user.uid)
              .child('vibes')
              .child(p.basename(file.path));

  String downloadUrl;

  TaskSnapshot uploadedFile = await refFeedBucket.putFile(file);

  if (uploadedFile.state == TaskState.success) {
    downloadUrl = await refFeedBucket.getDownloadURL();
  }

  return downloadUrl;
Run Code Online (Sandbox Code Playgroud)