Flutter在M1 Mac搭配iPhone真机用image_picker遇Firebase Storage错误求助
Flutter iOS端image_picker选图返回null+Firebase Storage异常排查求助
问题描述
我是Flutter新手,使用image_picker包从iOS相册选择图片时返回值为null,同时出现以下错误:
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: [firebase_storage/object-not-found] No object exists at the desired reference
查阅包文档得知模拟器存在已知问题,改用iPhone 14 Pro Max真机测试后仅成功过一次,恳请帮忙排查问题,如需更多信息请告知!
相关代码
String? photoUrl = " "; File? image; Future pickImage() async { try { final image = await ImagePicker() .pickImage(source: ImageSource.gallery, requestFullMetadata: false); if (image == null) return; Reference ref = FirebaseStorage.instance.ref().child("profilepic.jpg"); await ref.putFile(File(image.path)); await ref.getDownloadURL().then((value) {print(value); setState(() { photoUrl = value; });}); } on PlatformException catch (e) { debugPrint('Failed to pick image: $e'); } } profilePic() { final Size size = MediaQuery.of(context).size; return SizedBox( height: size.height * 0.185, width: double.infinity, child: Column(children: [ const Spacer(), photoUrl != null ? GestureDetector( onTap: () { pickImage(); }, child: Container( height: 120, width: 160, decoration: const BoxDecoration( color: Colors.blue, borderRadius: BorderRadius.all( Radius.circular(20), ), ), child: const Icon( Icons.person, size: 50, color: Colors.white, ), ), ):Image.network(photoUrl!), ], ), ); }
flutter doctor信息
[✓] Flutter (Channel stable, 3.7.0, on macOS 12.6.1 21G217 darwin-arm64, locale en-US) • Flutter version 3.7.0 on channel stable at /Users/myronsp/Developer/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision b06b8b2710 (9 days ago), 2023-01-23 16:55:55 -0800 • Engine revision b24591ed32 • Dart version 2.19.0 • DevTools version 2.20.1 [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0) • Android SDK at /Users/myronsp/Library/Android/sdk • Platform android-33, build-tools 33.0.0 • ANDROID_HOME = /Users/myronsp/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 14.2) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14C18 • CocoaPods version 1.11.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2022.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301) [✓] Connected device (5 available) • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64 • Android 13 (API 33) (emulator) • Myron’s iPhone (mobile) • 00008120-00016D500A80C01E • ios • iOS 16.0.2 20A380 • iPhone 14 Pro Max (mobile) • B8ACBFEA-35E5-4D2C-810C-46100D17EE63 • ios • com.apple.CoreSimulator.SimRuntime.iOS-16-2 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 12.6.1 21G217 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 109.0.5414.119
排查建议
1. 修复image_picker返回null问题
- 配置iOS相册权限:在
Info.plist中添加以下键值对,确保权限描述清晰:<key>NSPhotoLibraryUsageDescription</key> <string>需要访问相册以选择头像</string> <key>NSPhotoLibraryAddUsageDescription</key> <string>需要访问相册以保存图片</string> - 检查真机权限:进入iPhone「设置」→你的应用名称→「照片」,确认权限设置为「允许访问所有照片」
- 升级image_picker包:执行
flutter pub upgrade image_picker,使用最新稳定版适配iOS 16+系统
2. 解决Firebase Storage异常
- 使用唯一文件名:每次上传用不同文件名避免覆盖,同时防止上传失败后引用不存在,修改存储引用代码:
Reference ref = FirebaseStorage.instance.ref().child("profile_${DateTime.now().millisecondsSinceEpoch}.jpg"); - 捕获Firebase异常:补充异常捕获逻辑,打印详细错误信息:
} on FirebaseException catch (e) { debugPrint('Firebase Storage error: $e'); } - 检查存储规则:确保Firebase控制台的存储规则允许读写(测试阶段可临时开放,上线后需修改为安全规则):
rules_version = '2'; service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write: if request.auth != null; } } }
3. 修复UI逻辑错误
- 修正初始值和显示逻辑:
photoUrl初始值设为null,并反转显示判断逻辑,修改后代码片段:String? photoUrl = null; // ... photoUrl == null ? GestureDetector(/* 选择图片按钮 */) : Image.network(photoUrl!),
内容的提问来源于stack exchange,提问作者MSP
相关产品推荐
相关产品推荐

