Flutter项目引入crop_your_image包遇ExifData无data getter错误
Flutter引入crop_your_image包编译错误解决方案
问题
引入crop_your_image包后编译报错,错误信息如下:
: Error: The getter 'data' isn't defined for the class 'ExifData'. - 'ExifData' is from 'package:image/src/exif/exif_data.dart' ('../../../AppData/Local/Pub/Cache/hosted/pub.dev/image-3.3.0/lib/src/exif/exif_data.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'data'. switch (tempImage?.exif.data[0x0112] ?? -1) { ^^^^ Target kernel_snapshot failed: Exception FAILURE: Build failed with an exception. * Where: Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1151 * What went wrong: Execution failed for task ':app:compileFlutterBuildDebug'. > Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1 * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org
错误代码位于crop.dart第730行:
// decode orientation awared Image. image.Image _fromByteData(Uint8List data) { final tempImage = image.decodeImage(data); assert(tempImage != null); // check orientation switch (tempImage?.exif.data[0x0112] ?? -1) { // 报错行 case 3: return image.copyRotate(tempImage!, 180); case 6: return image.copyRotate(tempImage!, 90); case 8: return image.copyRotate(tempImage!, -90); } return tempImage!; }
删除crop_your_image包后错误消失,但仅当前开发项目出现该问题,其他使用该包的练习项目运行正常。
解决办法
这个错误是因为crop_your_image依赖的image库版本更新后,ExifData类的data属性被移除或重命名。当前项目使用的image库为3.3.0版本,而正常运行的项目大概率依赖的是保留data属性的旧版image库。
给出三种解决方向:
- 锁定image库版本:在
pubspec.yaml中指定image库为兼容的旧版本(比如2.1.19),然后执行flutter pub get更新依赖:
dependencies: # 保留其他原有依赖 image: 2.1.19 crop_your_image: ^你当前使用的包版本
- 临时修改包源码:找到本地缓存的crop_your_image包中
crop.dart文件,将报错行的代码替换为适配image 3.x版本的写法:
switch (tempImage?.exif.entries[0x0112]?.value ?? -1) {
注意:此方法在包版本更新后会失效,仅作为临时应急方案。
- 等待包官方更新:查看crop_your_image包的更新情况,若有适配image 3.x版本的新版本,直接升级包即可。
内容的提问来源于stack exchange,提问作者AdHoc
相关产品推荐
相关产品推荐

