运行Firebase图片上传代码时出现‘SkDeletable值为Null’报错有解决方案吗?
报错场景
开发过程中运行Banner图片上传关联代码时,抛出 Expected a value of type 'SkDeletable', but got one of type 'Null' 类型错误,相关参考截图为提问附带的报错页面截图。
原有业务代码
存储上传关联方法
Future uploadBannerImageToDb(url) async { String downloadUrl = await storge.ref(url).getDownloadURL(); if (downloadUrl != null) { firestore.collection('slider').add({ 'image': downloadUrl, }); } return downloadUrl; }
按钮点击触发逻辑
onPressed: () { progressDialog.show(); _services .uploadBannerImageToDb(_url) .then((downloadUrl) { if (downloadUrl != null) { progressDialog.dismiss(); _services.showMyDialog( title: 'New Banner Image', message: 'Saved Banner Image Successfully', context: context, ); } }); },
可行解决方案
- 校验存储路径合法性:检查传入
uploadBannerImageToDb的url参数,需与Firebase Storage中实际的文件存储路径完全匹配,路径不存在时getDownloadURL会返回空值,空地址传入图片渲染组件时就会触发SkDeletable类型的空值报错 - 增加异常捕获兜底:对
getDownloadURL的调用过程增加try-catch包裹,避免路径不存在、权限不足等场景下抛出的空值异常穿透到上层渲染层
示例兜底逻辑:Future uploadBannerImageToDb(url) async { String downloadUrl; try { downloadUrl = await storge.ref(url).getDownloadURL(); } catch (e) { // 存储异常时直接终止流程,关闭加载弹窗 progressDialog.dismiss(); return null; } if (downloadUrl != null) { await firestore.collection('slider').add({ 'image': downloadUrl, }); } return downloadUrl; } - 排查Web渲染兼容问题:该报错本质是Flutter Web端Skia渲染引擎收到了空的可销毁对象,多与依赖版本过低有关,可将
firebase_storage、cloud_firestore等SDK升级到最新稳定版,清除Web端运行缓存后重试 - 补齐异步错误捕获分支:给
uploadBannerImageToDb的Future调用链增加.catchError处理,避免未捕获的异步异常直接触发渲染层崩溃
示例补充逻辑:_services.uploadBannerImageToDb(_url) .then((downloadUrl) { // 原有成功判断逻辑 }) .catchError((err) { progressDialog.dismiss(); // 增加错误提示逻辑 });
内容的提问来源于stack exchange,提问作者mahmoud 2013
相关产品推荐
相关产品推荐

