flutter pub run build_runner build 时出错
Posted
技术标签:
【中文标题】flutter pub run build_runner build 时出错【英文标题】:Error on flutter pub run build_runner build 【发布时间】:2021-08-14 09:53:51 【问题描述】:我在运行 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
【问题讨论】:
【参考方案1】:在 pubspec yaml 的开发依赖项中添加分析器 https://pub.dev/packages/analyzer 依赖项 1.5.0 对我有用
dev_dependencies:
analyzer: 1.5.0
【讨论】:
【参考方案2】:如此处所述:https://github.com/dart-lang/sdk/issues/46136#issuecomment-848910733,您应该能够在不指定任何analyzer
版本的情况下只运行flutter pub upgrade
,它会起作用!
【讨论】:
【参考方案3】:尝试将这些行添加到您的 pubspec.yaml
dependency_overrides:
meta: 1.6.0
【讨论】:
谢谢。我暂时不想升级到最新版本。对于暂时不想升级到最新版本的任何人来说,这是一个很好的解决方案。【参考方案4】:dependency_overrides: 分析器:'1.5.0'
添加这个 pubspec.yaml 文件对我有用
【讨论】:
【参考方案5】:这似乎是最新版本的分析仪的问题。我可以通过将版本从 1.6.0 降级到 1.5.0 来修复它。
dev_dependencies: 分析器:1.5.0
【讨论】:
【参考方案6】:在我的构建运行程序中,我升级了构建运行程序和其他一些依赖项。
构建运行器以生成零件文件
build_runner: ^2.1.2
【讨论】:
【参考方案7】:将依赖项更新到最新版本。它对我有用。
auto_route:
build_runner:
auto_route_generator:
【讨论】:
以上是关于flutter pub run build_runner build 时出错的主要内容,如果未能解决你的问题,请参考以下文章
我迁移到空安全,我无法运行“flutter pub run build_runner build”而不抛出错误
“flutter pub run build_runner watch --delete-conflicting-outputs”的 VS Code 键盘快捷键