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

向Firebase Storage上传图片持续报错,寻求排查解决方法

Firebase Storage图片上传失败问题

尝试将图片上传至Firebase Storage时持续报错,以下是相关代码及信息:

页面逻辑代码

Future selectFile() async {
  final result = await ImagePicker.platform.pickImage(source: ImageSource.gallery);

  if (result == null) return;
  final path = result.path;

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

Future uploadFile() async {
  if (file == null) return;

  final fileName = file!.path.split("/").last;
  final destination = "test/";

  Storage.uploadFile(destination, file!);
}

Storage工具类代码

import 'dart:io';

import 'package:firebase_storage/firebase_storage.dart';

class Storage {
  static Future<void> uploadFile(String destination, File file) async {
    try {
      final ref = FirebaseStorage.instance.ref().child(destination);

      await ref.putFile(file);
    } on FirebaseException catch (e) {
      print(e);
    }
  }
}

报错信息

E/StorageException(27972):  at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:272)
E/StorageException(27972):  at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:289)
E/StorageException(27972):  at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:76)
E/StorageException(27972):  at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:68)
E/StorageException(27972):  at com.google.firebase.storage.UploadTask.sendWithRetry(UploadTask.java:477)
E/StorageException(27972):  at com.google.firebase.storage.UploadTask.beginResumableUpload(UploadTask.java:276)
E/StorageException(27972):  at com.google.firebase.storage.UploadTask.run(UploadTask.java:224)
E/StorageException(27972):  ... 5 more
I/flutter (27972): [firebase_storage/object-not-found] No object exists at the desired reference.

当前Firebase存储规则

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

更新进展

用上述代码新建了一个仅含两个按钮的Flutter应用,代码可正常运行,推测原问题可能出在原项目的依赖或配置层面,后续解决后会同步更新。


排查建议

  • 核对Firebase配置文件:确认原项目Android端的google-services.json或iOS端的GoogleService-Info.plist配置正确,重点检查存储Bucket地址是否与新应用一致。
  • 统一依赖版本:对比新应用和原项目pubspec.yaml中firebase_storage、firebase_core等Firebase相关依赖的版本,尝试升级或降级到能正常运行的版本。
  • 清理项目缓存:执行flutter clean命令后重新编译运行原项目,排除缓存导致的异常。
  • 检查网络权限:确认原项目AndroidManifest.xml(Android)或Info.plist(iOS)中已添加网络访问权限,上传操作需要稳定网络连接。
  • 捕获完整错误信息:在Storage.uploadFile方法中,打印FirebaseException的所有属性(如e.code、e.message、e.details),获取更精准的错误线索。

内容的提问来源于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:06:33