You need to enable JavaScript to run this app. 运行Firebase图片上传代码时出现‘SkDeletable值为Null’报错有解决方案吗?
优惠活动
大模型
产品
解决方案
定价
更多

运行Firebase图片上传代码时出现‘SkDeletable值为Null’报错有解决方案吗?

Firebase Banner图片上传报错处理

报错场景

开发过程中运行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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.29 05:36:02