Flutter:如何修复IgnoreLeaks包相关报错?
解决leak_tracker_testing与leak_tracker版本不兼容导致的notGCed错误
问题背景
开发AR应用时引入firebase_core和model_viewer_plus: ^1.7.0后,出现依赖版本冲突,报错显示leak_tracker_testing调用的notGCed参数及getter在leak_tracker: ^10.0.4中未定义,尝试flutter clear、flutter pub get及手动清理缓存无效。
报错详情
Launching lib\main.dart on sdk gphone x86 in debug mode... Building with Flutter multidex support enabled. ../../../../../Pub/Cache/hosted/pub.dev/leak_tracker_testing-2.0.3/lib/src/leak_testing.dart:146:9: Error: No named parameter with the name 'notGCed'. notGCed: ignoredLeaks.notGCed.merge( ^^^^^^^ ../../../../../Pub/Cache/hosted/pub.dev/leak_tracker-10.0.4/lib/src/leak_tracking/primitives/model.dart:130:9: Context: Found this candidate, but the arguments don't match. const IgnoredLeaks({ ^^^^^^^^^^^^ ../../../../../Pub/Cache/hosted/pub.dev/leak_tracker_testing-2.0.3/lib/src/leak_testing.dart:193:9: Error: No named parameter with the name 'notGCed'. notGCed: newNotGCed, ^^^^^^^ ../../../../../Pub/Cache/hosted/pub.dev/leak_tracker-10.0.4/lib/src/leak_tracking/primitives/model.dart:130:9: Context: Found this candidate, but the arguments don't match. const IgnoredLeaks({ ^^^^^^^^^^^^ ../../../../../Pub/Cache/hosted/pub.dev/leak_tracker_testing-2.0.3/lib/src/leak_testing.dart:180:35: Error: The getter 'notGCed' isn't defined for the class 'IgnoredLeaks'. - 'IgnoredLeaks' is from 'package:leak_tracker/src/leak_tracking/primitives/model.dart' ('../../../../../Pub/Cache/hosted/pub.dev/leak_tracker-10.0.4/lib/src/leak_tracking/primitives/model.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'notGCed'. var newNotGCed = ignoredLeaks.notGCed.track([...notGCed, ...classes]); ^^^^^^^ Target kernel_snapshot failed: Exception FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:compileFlutterBuildDebug'. > Process 'command '..\..\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 BUILD FAILED in 1m 1s Error: Gradle task assembleDebug failed with exit code 1 Exited (1).
解决方案
方法一:强制指定兼容的版本组合
在pubspec.yaml中添加dependency_overrides,强制使用匹配的版本对:
选项1:降级leak_tracker到9.x(适配现有leak_tracker_testing 2.x)
dependency_overrides: leak_tracker: ^9.0.3 leak_tracker_testing: ^2.0.3
选项2:升级leak_tracker_testing到3.x(适配leak_tracker 10.x)
dependency_overrides: leak_tracker_testing: ^3.0.1
添加后执行:
flutter pub get
方法二:彻底清理依赖缓存并重新拉取
如果方法一无效,执行以下命令彻底清理缓存:
flutter pub cache clean flutter pub get
原因说明
leak_tracker在10.0.0版本对IgnoredLeaks类进行了API重构,移除了notGCed参数及getter,替换为retained;而leak_tracker_testing 2.x版本仍依赖旧API。你的项目中firebase_core或model_viewer_plus间接引入了版本不匹配的两个包,导致编译冲突。
内容的提问来源于stack exchange,提问作者0-shiva
相关产品推荐
相关产品推荐

