我在运行 flutter pub run build_runner build 时遇到了这个问题。我尝试过类似 flutter clean、flutter pub-cache repair、flutter pub get 等解决方案。在 pubspec 上添加了 Mockito、HTTP 和 build_runner 等依赖项,flutter pub get 也成功运行。
Failed to precompile build_runner:build_runner:
../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-1.6.0/lib/src/error/best_practices_verifier.dart:1600:40: Error: Getter not found: 'topLevelVariable'.
return kinds.contains(TargetKind.topLevelVariable);
^^^^^^^^^^^^^^^^
../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-1.6.0/lib/src/error/best_practices_verifier.dart:2024:23: Error: Getter not found: 'topLevelVariable'.
case TargetKind.topLevelVariable:
^^^^^^^^^^^^^^^^
../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-1.6.0/lib/src/error/best_practices_verifier.dart:2024:23: Error: Type 'dynamic' of the case expression is not a subtype of type 'TargetKind' of this switch expression.
- 'TargetKind' is from 'package:meta/meta_meta.dart' ('../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/meta-1.3.0/lib/meta_meta.dart').
case TargetKind.topLevelVariable:
^
../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-1.6.0/lib/src/error/best_practices_verifier.dart:2001:13: Context: The switch expression is here.
switch (this) {
^
pub finished with exit code 1
答案 0 :(得分:7)
在 pubspec yaml 的开发依赖项中添加分析器 https://pub.dev/packages/analyzer 依赖项 1.5.0 对我有用
dev_dependencies:
analyzer: 1.5.0
答案 1 :(得分:1)
如此处所述:https://github.com/dart-lang/sdk/issues/46136#issuecomment-848910733,您应该能够在不指定任何 flutter pub upgrade
版本的情况下运行 analyzer
,并且它会起作用!
答案 2 :(得分:0)
dependency_overrides: 分析器:'1.5.0'
添加这个 pubspec.yaml 文件对我有用
答案 3 :(得分:0)
这似乎是最新版本的分析仪的问题。我可以通过将版本从 1.6.0 降级到 1.5.0 来修复它。
dev_dependencies: 分析器:1.5.0