You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase Storage新应用正常原应用异常,报错[firebase_storage/object-not-found]

问题原因及修复方案

可能的原因

  • Firebase配置不一致:原应用的Firebase配置文件(Android的google-services.json或iOS的GoogleService-Info.plist)可能指向错误的Storage Bucket,或是配置文件未正确更新,导致上传请求发送到了不存在的存储位置。
  • 状态变量异常:原应用中可能存在其他业务逻辑干扰了file或fileName的状态,比如fileName被意外重置为空值,导致上传路径test/${fileName}变为无效路径(如test/null),触发“对象不存在”的错误(实际是路径无效导致的上传失败)。
  • Storage规则限制:原应用的Firebase Storage规则可能设置了严格的访问限制,比如仅允许特定用户或路径上传,而新应用使用了默认宽松规则,因此能正常上传。

修复步骤

1. 验证Firebase配置

  • 对比原应用和测试应用的Firebase配置文件,确保两者的Storage Bucket地址完全一致。
  • 重新下载最新的配置文件替换原应用中的旧文件,避免配置过期或错误。

2. 增强代码健壮性

修改selectFile和uploadFile方法,添加非空校验和错误监听:

Future selectFile() async {
  final result = await FilePicker.platform.pickFiles(
    allowMultiple: false,
    type: FileType.custom,
    allowedExtensions: ["png", "jpg"],
  );

  if (result == null || result.files.isEmpty) return;

  final path = result.files.single.path;
  final name = result.files.single.name;

  if (path == null || name.isEmpty) return;

  print(path);
  print(name);

  setState(() {
    file = File(path);
    fileName = name;
  });
}

Future uploadFile() async {
  if (file == null || fileName == null || fileName!.isEmpty) {
    print("文件或文件名无效");
    return;
  }

  final destination = "test/${fileName!}";
  final storageRef = FirebaseStorage.instance.ref(destination);

  try {
    await storageRef.putFile(file!);
    print("上传成功");
    // 可添加获取下载链接的逻辑
    // final url = await storageRef.getDownloadURL();
    // print(url);
  } on FirebaseException catch (e) {
    print("上传失败:${e.message}");
    // 根据错误类型做对应处理
  }
}

3. 检查Storage规则

临时将原应用的Storage规则改为宽松模式测试(仅用于排查,生产环境需修改为安全规则):

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if true;
    }
  }
}

如果宽松规则下能正常上传,说明原规则限制了上传权限,需调整规则以适配业务需求。

4. 排查状态干扰

检查原应用中是否有其他代码修改了file或fileName变量,比如页面跳转、其他状态更新逻辑导致这两个变量被意外重置,确保选择文件后这两个变量的值能持续保留到上传操作执行时。

内容的提问来源于stack exchange,提问作者Potato

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 20:24:32