Flutter+Firebase在模拟器/TestFlight正常,苹果审核上传报错
Firebase Flutter应用苹果审核上传报错排查求助
我测试了集成Firebase的Flutter应用,在模拟器、真机以及TestFlight中均运行正常,但苹果审核人员尝试向Firebase上传内容时持续报错,错误信息仅为:an unknown error occurred please check the server response。
报错发生在以下代码段:
Future<Memory?> addMemory({ required BuildContext context, required String title, required List<String> imageFilePaths, required DateTime dateTime, required String? story, int? sortIndexIfEditing, }) async { try { Year currentYear = Provider.of<DataProvider>(context, listen: false).getCurrentYear(); Month currentMonth = Provider.of<DataProvider>(context, listen: false).getCurrentMonth(); int currentMonthIndex = currentMonth.month; String uid = AuthService.currentUser!.uid; List<String> imageLocations = []; for (String path in imageFilePaths) { String uidForLocation = Uuid().v1(); String imageLocation = '$uid/${currentYear.id}/${currentMonth.id}/$uidForLocation'; await FirebaseStorage.instance.ref(imageLocation).putFile( File(path), ); imageLocations.add(imageLocation); } Memory? memoryToReturn = await _addMemoryToDatabse( context: context, imageLocations: imageLocations, currentYear: currentYear, currentMonthIndex: currentMonthIndex, title: title, dateTime: dateTime, story: story, sortIndexIfEditing: sortIndexIfEditing, ); return memoryToReturn; } on FirebaseException catch (e) { print('addMemory failed: ' + e.message.toString()); AlertService.showSnackBar( title: 'Ups, hier ist etwas schiefgelaufen...', description: e.message.toString(), isSuccess: false, ); return null; } }
我已排查过Firestore的安全规则,确认不是该问题,规则如下:
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if request.auth != null; } } }
可能的排查方向:
- Firebase Storage权限遗漏:你只提供了Firestore的规则,但报错发生在Storage上传环节,检查Storage的安全规则是否允许已认证用户写入,示例规则:
rules_version = '2'; service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write: if request.auth != null; } } } - 网络访问限制:苹果审核环境的网络可能存在防火墙或代理,导致无法正常访问Firebase存储端点。建议在代码中扩大异常捕获范围,捕获所有类型的异常并打印完整错误栈(包括错误代码、详细信息),而不只是FirebaseException的message。
- 文件读取权限问题:审核设备上的目标图片文件是否能被App正常读取?检查iOS相册权限申请是否完整,上传前增加文件存在性和可读权限的校验逻辑。
- UUID生成异常:代码中使用
Uuid().v1()生成路径,v1版本依赖设备MAC地址,审核设备的环境可能导致UUID生成失败,可尝试替换为Uuid().v4()版本。 - Firebase区域配置不一致:确认Firebase项目中Storage和Firestore的区域配置一致,避免跨区域访问带来的连接问题。
- 异常捕获范围不足:当前代码仅捕获FirebaseException,可能存在IO异常、Provider状态异常等其他类型错误,建议修改为
catch (e, stackTrace)来捕获所有异常,并输出完整的错误信息辅助定位。
若需要自行测试,可告知邮箱获取TestFlight权限。
内容的提问来源于stack exchange,提问作者Chris
相关产品推荐
相关产品推荐

