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

Flutter中用FlutterNativeImage压缩图片遇MissingPluginException问题求助

解决FlutterNativeImage压缩图片时的MissingPluginException错误
  • 检查依赖配置
    确认pubspec.yaml中已正确添加flutter_native_image依赖,并执行flutter pub get拉取包:

    dependencies:
      flutter_native_image: ^1.1.6 # 建议使用最新版本
    
  • 重新构建项目(核心修复步骤)
    该错误多数是插件未正确关联到原生层,执行以下操作:

    1. 停止当前运行的模拟器/真机进程
    2. 执行flutter clean清理项目缓存
    3. 重新执行flutter pub get
    4. 重新启动项目flutter run
      若为iOS项目,可额外进入ios目录执行pod install,再重新运行
  • 校验方法调用参数
    确认scannedImage是有效的File对象,路径可访问。可先打印scannedImage!.path验证路径是否正确:

    print(scannedImage!.path); // 检查路径是否存在
    File? compressedFile = await FlutterNativeImage.compressImage(
      scannedImage!.path,
      quality: 20,
      percentage: 60);
    
  • 替代方案(若上述步骤无效)
    可换用flutter_image_compress插件实现压缩,示例代码:

    import 'package:flutter_image_compress/flutter_image_compress.dart';
    import 'dart:io';
    import 'package:path_provider/path_provider.dart';
    
    Future<File?> compressImage(File image) async {
      final tempDir = await getTemporaryDirectory();
      final targetPath = '${tempDir.path}/compressed_${DateTime.now().millisecondsSinceEpoch}.jpg';
      final result = await FlutterImageCompress.compressAndGetFile(
        image.absolute.path,
        targetPath,
        quality: 20,
        minWidth: (image.width * 0.6).toInt(),
        minHeight: (image.height * 0.6).toInt(),
      );
      return result;
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 14:52:06