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

iOS端Flutter应用调用image_picker时崩溃问题求助

iOS Flutter应用调用image_picker时崩溃问题

问题描述

在iOS端Flutter应用中调用image_picker选择图片时,应用立即崩溃:模拟器控制台显示“lost connection to device”后崩溃,真机直接停止运行。已在info.plist中添加所需权限参数,且使用最新版image_picker和Flutter,但问题仍未解决。

相关代码配置

info.plist内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>CFBundleDevelopmentRegion</key>
        <string>$(DEVELOPMENT_LANGUAGE)</string>
        <key>CFBundleExecutable</key>
        <string>$(EXECUTABLE_NAME)</string>
        <key>CFBundleIconFile</key>
        <string></string>
        <key>CFBundleIdentifier</key>
        <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
        <key>CFBundleInfoDictionaryVersion</key>
        <string>6.0</string>
        <key>CFBundleName</key>
        <string>$(PRODUCT_NAME)</string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleShortVersionString</key>
        <string>$(FLUTTER_BUILD_NAME)</string>
        <key>CFBundleVersion</key>
        <string>$(FLUTTER_BUILD_NUMBER)</string>
        <key>LSMinimumSystemVersion</key>
        <string>$(MACOSX_DEPLOYMENT_TARGET)</string>
        <key>NSHumanReadableCopyright</key>
        <string>$(PRODUCT_COPYRIGHT)</string>
        <key>NSMainNibFile</key>
        <string>MainMenu</string>
        <key>NSPrincipalClass</key>
        <string>NSApplication</string>
        <key>NSCameraUsageDescription</key>
        <string>Access to take a photo by camera</string>
        <string>Access to pick a photo</string>
        <key>NSPhotoLibraryUsageDescription</key>
        <string>Access to pick a photo</string>
        <key>NSAppleMusicUsageDescription</key>
        <string>Access to pick a photo</string>
    </dict>
</plist>

ImagePicker服务代码

import 'package:image_picker/image_picker.dart';

class ImageService {
  static Future pickImage() async {
    final ImagePicker picker = ImagePicker();
    final image = await picker.pickImage(source: ImageSource.gallery);
  
    return image;
  }
}

调用位置代码

return Padding(
  padding: const EdgeInsets.only(right: 16.0),
  child: InkWell(
    onTap: () async {
      final image = await ImageService.pickImage();
      ctrl.imagesAddedToVisa.add(image);
    },
    child: SizedBox(
      height: 80,
      width: 150,
      child: DottedBorder(
          color: AppColors.mainTint,
          radius: const Radius.circular(16),
          dashPattern: const [5, 16],
          strokeWidth: 3,
          strokeCap: StrokeCap.round,
          child: Padding(
            padding: const EdgeInsets.all(16),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Icon(
                  FontAwesomeIcons.fileCirclePlus,
                  color: AppColors.mainColor,
                  size: 35,
                ),
                const SizedBox(
                  height: 15,
                ),
                Flexible(
                  child: Text(
                    'add_passport_photo'.tr,
                    maxLines: 2,
                    softWrap: true,
                    overflow: TextOverflow.fade,
                  ),
                ),
              ],
            ),
          )),
    ),
  ),
);

解决方案

  • 修复info.plist格式错误:NSCameraUsageDescription键下存在两个<string>值,违反plist键值对规则(一个键仅能对应一个值)。删除多余的<string>Access to pick a photo</string>,保留正确的相机权限描述:
    <key>NSCameraUsageDescription</key>
    <string>Access to take a photo by camera</string>
    
  • 移除无关权限:NSAppleMusicUsageDescription与图片选择功能无关,建议删除该键值对,避免不必要的权限声明。
  • 清理重建项目:
    1. 执行flutter clean清理项目缓存
    2. 删除iOS目录下的Pods文件夹和Podfile.lock文件
    3. 进入iOS目录执行pod install
    4. 重新运行应用
  • 添加空值判断:picker.pickImage()在用户取消选择时会返回null,在调用处添加判断避免向列表中添加null引发错误:
    onTap: () async {
      final image = await ImageService.pickImage();
      if (image != null) {
        ctrl.imagesAddedToVisa.add(image);
      }
    },
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 21:59:58