我的应用程序从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
发布于 2021-02-08 10:55:07
根据图像的大小,上传文件所需的时间会有所不同。所以你的错误很可能是由于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;
}
发布于 2021-05-22 20:56:24
抛出错误是因为您正在尝试访问尚未创建的下载url。
您可以将上传代码封装在一个if语句中,如下面的示例所示。这样可以保证上传任务已经成功完成。
if(storageRef
.child(folderName)
.putFile(fileName).isSuccessful)
{
url = await storage.child("folderName").child(fileName).getDownloadURL();
}
发布于 2021-11-17 05:11:42
您可以检查对象是否存在,如下所示:
import 'package:firebase_core/firebase_core.dart' as firebase_core;
try {
FirebaseStorage.instance
.ref()
.child(widget.snap[index].data['英文品名']+".jpg")
.getDownloadURL()
} on firebase_core.FirebaseException catch(error) {
// if the Object does not exists
if (error.code == 'object-not-found')) {
//DO something
}
}
https://stackoverflow.com/questions/58410497
复制相似问题