执行dart run build_runner时因custom_lint报错崩溃的求助
问题详情
执行build_runner时出现崩溃,核心报错为NullAwareElement类型未找到,具体错误信息如下:
[WARNING] ../../../.pub-cache/hosted/pub.dev/custom_lint_visitor-1.0.0+6.11.0/lib/src/node_lint_visitor.g.dart:816:30: Error: Type 'NullAwareElement' not found. void visitNullAwareElement(NullAwareElement node) { ^^^^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/custom_lint_visitor-1.0.0+6.11.0/lib/src/node_lint_visitor.g.dart:2156:28: Error: Type 'NullAwareElement' not found. final List<_Subscription<NullAwareElement>> _forNullAwareElement = []; ^^^^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/custom_lint_visitor-1.0.0+6.11.0/lib/src/node_lint_visitor.g.dart:2158:33: Error: Type 'NullAwareElement' not found. String key, void Function(NullAwareElement node) listener) { ^^^^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/custom_lint_visitor-1.0.0+6.11.0/lib/src/node_lint_visitor.g.dart:3359:42: Error: Type 'NullAwareElement' not found. void addNullAwareElement(void Function(NullAwareElement node) listener) { ^^^^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/custom_lint_visitor-1.0.0+6.11.0/lib/src/node_lint_visitor.g.dart:816:30: Error: 'NullAwareElement' isn't a type. void visitNullAwareElement(NullAwareElement node) { ^^^^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/custom_lint_visitor-1.0.0+6.11.0/lib/src/node_lint_visitor.g.dart:818:11: Error: Superclass has no method named 'visitNullAwareElement'. super.visitNullAwareElement(node); ^^^^^^^^^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/custom_lint_visitor-1.0.0+6.11.0/lib/src/node_lint_visitor.g.dart:2156:28: Error: 'NullAwareElement' isn't a type. final List<_Subscription<NullAwareElement>> _forNullAwareElement = []; ^^^^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/custom_lint_visitor-1.0.0+6.11.0/lib/src/node_lint_visitor.g.dart:2158:33: Error: 'NullAwareElement' isn't a type. String key, void Function(NullAwareElement node) listener) { ^^^^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/custom_lint_visitor-1.0.0+6.11.0/lib/src/node_lint_visitor.g.dart:3359:42: Error: 'NullAwareElement' isn't a type. void addNullAwareElement(void Function(NullAwareElement node) listener) { ^^^^^^^^^^^^^^^^ [INFO] Precompiling build script... completed, took 510ms [SEVERE] Failed to precompile build script .dart_tool/build/entrypoint/build.dart. This is likely caused by a misconfigured builder definition.
已尝试的操作:
- 执行
flutter pub cache repair、flutter pub cache clean、flutter clean命令 - 更换过旧版本依赖
当前依赖版本:
build_runner: ^2.4.13 isar_generator: *isar_version riverpod_generator: ^2.6.2 custom_lint: ^0.7.0 riverpod_lint: ^2.6.2
Flutter环境信息:
Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.24.4, on macOS 15.1 24B83 darwin-arm64, locale pt-BR) [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 16.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2023.2) [✓] VS Code (version 1.95.2) [✓] Connected device (4 available) [✓] Network resources
解决方案
这个问题核心是custom_lint与相关依赖(尤其是riverpod_lint、dart_analyzer)版本不兼容,NullAwareElement是dart_analyzer中的类型,版本不匹配会导致custom_lint_visitor无法找到该类型。可尝试以下步骤:
对齐custom_lint与riverpod_lint版本
riverpod_lint 2.6.2对custom_lint有特定版本要求,建议将custom_lint升级到^0.8.0(可查看pub.dev上riverpod_lint的兼容版本说明),修改pubspec.yaml后执行:flutter clean rm pubspec.lock flutter pub get锁定dart_analyzer版本(临时方案)
若上述方法无效,可在pubspec.yaml中添加版本约束,确保dart_analyzer与custom_lint_visitor兼容:dependency_overrides: dart_analyzer: ^6.11.0之后重新执行
flutter pub get和build_runner命令。统一更新生成器依赖
将build_runner、riverpod_generator、riverpod_lint、custom_lint都更新到当前Flutter稳定版支持的最新版本,执行:flutter pub upgrade build_runner riverpod_generator riverpod_lint custom_lint flutter clean flutter pub get明确isar_generator版本
isar_generator: *isar_version的变量引用可能导致版本不匹配,建议替换为具体版本号,确保与当前Flutter版本兼容。
内容的提问来源于stack exchange,提问作者Evandro Junior

