Flutter Firebase Storage -文件不存在时隐藏存储异常

jv4diomz  于 2023-03-31  发布在  Flutter
关注(0)|答案(2)|浏览(151)

我正在为一个应用程序使用Flutter和Firebase Storage。我试图创建一个函数来知道文件是否存在:

Future<bool> exists(String path) async {
   bool _exists;
   StorageReference storageReference = FirebaseStorage.instance.ref().child(path);
   try {
      await storageReference.getDownloadURL();
      _exists = true;
   } catch (exception) {
      _exists = false;
   }
   return _exists;
}

它正在工作,但当文件不存在时,它仍然记录错误:

I/System.out(12012): (HTTPLog)-Static: isSBSettingEnabled false
E/StorageException(12012): StorageException has occurred.
E/StorageException(12012): Object does not exist at location.
E/StorageException(12012):  Code: -13010 HttpResult: 404
E/StorageException(12012): {  "error": {    "code": 404,    "message": "Not Found.  Could not get object",    "status": "GET_OBJECT"  }}
E/StorageException(12012): java.io.IOException: {  "error": {    "code": 404,    "message": "Not Found.  Could not get object",    "status": "GET_OBJECT"  }}
E/StorageException(12012):  at com.google.firebase.storage.network.NetworkRequest.parseResponse(com.google.firebase:firebase-storage@@17.0.0:455)
E/StorageException(12012):  at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(com.google.firebase:firebase-storage@@17.0.0:435)
E/StorageException(12012):  at com.google.firebase.storage.network.NetworkRequest.processResponseStream(com.google.firebase:firebase-storage@@17.0.0:426)
E/StorageException(12012):  at com.google.firebase.storage.network.NetworkRequest.performRequest(com.google.firebase:firebase-storage@@17.0.0:280)
E/StorageException(12012):  at com.google.firebase.storage.network.NetworkRequest.performRequest(com.google.firebase:firebase-storage@@17.0.0:294)
E/StorageException(12012):  at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(com.google.firebase:firebase-storage@@17.0.0:70)
E/StorageException(12012):  at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(com.google.firebase:firebase-storage@@17.0.0:62)
E/StorageException(12012):  at com.google.firebase.storage.GetDownloadUrlTask.run(com.google.firebase:firebase-storage@@17.0.0:74)
E/StorageException(12012):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
E/StorageException(12012):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
E/StorageException(12012):  at java.lang.Thread.run(Thread.java:764)

正因为如此,控制台现在充满了这些行,调试其他任何东西都变得非常困难。
我怎么能隐藏这些线?
谢谢大家!

kxe2p93d

kxe2p93d1#

捕获这样的错误并处理它

}, onError: (e) {
        debugPrint('Exception::: ==========>>>>>>> ${e.toString()}');
      });
kmpatx3s

kmpatx3s2#

您可以通过StorageException捕获“对象不存在”的错误,并简单地返回false而不记录日志,就像这样

Future<bool> exists(String path) async {
   bool _exists;
   StorageReference storageReference = FirebaseStorage.instance.ref().child(path);
   try {
      await storageReference.getDownloadURL();
      _exists = true;
   } on StorageException catch (e) {
      if (e.code == StorageErrorCode.objectNotFound) {
         _exists = false;
      } else {
         rethrow;
      }
   }
   return _exists;
}

相关问题