Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何处理Firebase Storage StorageException_Firebase_Flutter_Dart_Firebase Storage - Fatal编程技术网

如何处理Firebase Storage StorageException

如何处理Firebase Storage StorageException,firebase,flutter,dart,firebase-storage,Firebase,Flutter,Dart,Firebase Storage,我的应用程序从Firebase存储获取图像。如果没有图像,我希望能够处理错误。但我似乎无法让它工作 我试过用try-catch环绕 我试过这个 Future<dynamic> getImage(int index){ return FirebaseStorage.instance.ref().child(widget.snap[index].data['英文品名']+".jpg").getDownloadURL().catchError((onError){

我的应用程序从Firebase存储获取图像。如果没有图像,我希望能够处理错误。但我似乎无法让它工作

我试过用try-catch环绕

我试过这个

Future<dynamic> getImage(int index){
      return FirebaseStorage.instance.ref().child(widget.snap[index].data['英文品名']+".jpg").getDownloadURL().catchError((onError){
        print(onError);
      }); 
 }
如何处理此异常?

根据图像的大小,上载文件需要不同的时间。因此,您的错误很可能是由于async和Wait的错误组合造成的。这个代码对我有用

Future<String> uploadSingleImage(File file) async {
    //Set File Name
    String fileName = DateTime.now().millisecondsSinceEpoch.toString() +
        AuthRepository.getUser().uid +
        '.jpg';
    
    //Create Reference
    Reference reference = FirebaseStorage.instance
        .ref()
        .child('Single Post Images')
        .child(fileName);

    //Now We have to check status of UploadTask
    UploadTask uploadTask = reference.putFile(file);
    
    String url;
    await uploadTask.whenComplete(() async {
      url = await uploadTask.snapshot.ref.getDownloadURL();
    });
    print('before return');
    return url;
  }
Future uploadSingleImage(文件)异步{
//设置文件名
字符串文件名=DateTime.now().millissecondssinceepoch.toString()+
AuthRepository.getUser().uid+
"jpg",;
//创建引用
Reference=FirebaseStorage.instance
.ref()
.child('Single Post image')
.child(文件名);
//现在我们必须检查UploadTask的状态
UploadTask UploadTask=reference.putFile(文件);
字符串url;
等待上载任务。完成时(()异步{
url=wait uploadTask.snapshot.ref.getDownloadURL();
});
打印(“返回前”);
返回url;
}

由于您试图在尚未创建下载url时访问该url,因此引发该错误

您可以将上传代码封装在一个if语句中,如下面的示例所示。这样可以确保上载任务已成功完成

 if(storageRef
        .child(folderName)
        .putFile(fileName).isSuccessful)
      {
        url = await storage.child("folderName").child(fileName).getDownloadURL();
      }

我不太明白。你已经捕获到错误,那么你想在那里添加什么?@FrankvanPuffelen,问题是即使我添加了捕获,异常仍然没有被捕获。就好像我一开始就没有添加捕获物一样。我在VSCODE中添加了一个异常的图像。我在库的代码中查看了一下,但是我也找不到如何做到这一点。我想问一下。嗨,你能试着用wait来代替吗?这样你就可以确定“getDownloadUrl”在你的try-catch中确实完成了吗?类似于“String url=await ref.getDownloadUrl();”这样,您就可以确定错误发生在try-catch中,并且应该被捕获。也许这会让事情变得更清楚。还要注意的是,即使在Dart端捕捉到错误,您也会在控制台中看到StorageException输出。因此,您的问题中的堆栈跟踪片段是预期的。您共享的映像中的错误是捕获存储插件返回的错误的“我认为”VSCode。尝试在错误处理程序中打印其他内容,可能它最终会被捕获。
Future<String> uploadSingleImage(File file) async {
    //Set File Name
    String fileName = DateTime.now().millisecondsSinceEpoch.toString() +
        AuthRepository.getUser().uid +
        '.jpg';
    
    //Create Reference
    Reference reference = FirebaseStorage.instance
        .ref()
        .child('Single Post Images')
        .child(fileName);

    //Now We have to check status of UploadTask
    UploadTask uploadTask = reference.putFile(file);
    
    String url;
    await uploadTask.whenComplete(() async {
      url = await uploadTask.snapshot.ref.getDownloadURL();
    });
    print('before return');
    return url;
  }
 if(storageRef
        .child(folderName)
        .putFile(fileName).isSuccessful)
      {
        url = await storage.child("folderName").child(fileName).getDownloadURL();
      }