gpt4 book ai didi

firebase - 如何处理 Firebase Storage StorageException

转载 作者:行者123 更新时间:2023-12-04 04:23:41 24 4
gpt4 key购买 nike

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

我试过用 try catch 包围。

我试过这个

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

和这个
 Future<dynamic> getImage(int index){
var imageStream;
try {
imageStream = FirebaseStorage.instance.ref().child(widget.snap[index].data['英文品名']+".jpg").getDownloadURL();
} catch (e) {
print(e);
}
return imageStream;
}

但我总是收到未处理的异常错误并且我的应用程序崩溃。
E/StorageException(11819): StorageException has occurred.
E/StorageException(11819): Object does not exist at location.
E/StorageException(11819): Code: -13010 HttpResult: 404
E/StorageException(11819): StorageException has occurred.
E/StorageException(11819): Object does not exist at location.
E/StorageException(11819): Code: -13010 HttpResult: 404
E/StorageException(11819): { "error": { "code": 404, "message": "Not Found. Could not get object", "status": "GET_OBJECT" }}
E/StorageException(11819): java.io.IOException: { "error": { "code": 404, "message": "Not Found. Could not get object", "status": "GET_OBJECT" }}

如何处理这个异常?
Image of exception in VS Code

最佳答案

根据图像的大小,上传文件将花费不同的时间。所以很可能你的错误是因为 async 和 await 的错误组合。这段代码对我有用。

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

关于firebase - 如何处理 Firebase Storage StorageException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58410497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com