如何修复 Flutter build_runner 错误:无法预编译 build_runner:build_runner:

Posted

技术标签:

【中文标题】如何修复 Flutter build_runner 错误:无法预编译 build_runner:build_runner:【英文标题】:How to fix Flutter build_runner error: Failed to precompile build_runner:build_runner: 【发布时间】:2021-08-03 17:19:15 【问题描述】:

我正在开发两个 Flutter 项目,其中一个已经迁移到 null 安全并使用 Flutter 2.0.5。第二个项目,即导致问题的项目,尚未迁移到 null 安全性并使用 Flutter 1.22.6。只需重命名 Flutter SDK 文件夹即可切换项目。

现在的问题是 build_runner 仅适用于 Flutter 2.0.5。从 Flutter 1.22.6 项目内部调用 flutter pub run build_runner build 时,失败 带有以下错误消息:

Failed to precompile build_runner:build_runner:
../../../Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/http_multi_server-2.1.0/lib/src/multi_headers.dart:97:8: Error: The method 'MultiHeaders.add' has fewer named arguments than those of overridden method 'HttpHeaders.add'.
  void add(String name, Object value) 
       ^
org-dartlang-sdk:///sdk/lib/_http/http.dart:698:8: Context: This is the overridden method ('add').
  void add(String name, Object value,
       ^
../../../Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/http_multi_server-2.1.0/lib/src/multi_headers.dart:129:8: Error: The method 'MultiHeaders.set' has fewer named arguments than those of overridden method 'HttpHeaders.set'.
  void set(String name, Object value) 
       ^
org-dartlang-sdk:///sdk/lib/_http/http.dart:707:8: Context: This is the overridden method ('set').
  void set(String name, Object value,
       ^
../../../Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/build_runner-1.11.0/lib/src/entrypoint/runner.dart:23:31: Error: No named parameter with the name 'usageLineLength'.
  final argParser = ArgParser(usageLineLength: lineLength);
                              ^^^^^^^^^^^^^^^
../../../Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/args-1.4.4/lib/src/arg_parser.dart:43:11: Context: Found this candidate, but the arguments don't match.
  factory ArgParser(bool allowTrailingOptions: true) =>
          ^
../../../Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/build_runner-1.11.0/bin/src/commands/clean.dart:18:31: Error: No named parameter with the name 'usageLineLength'.
  final argParser = ArgParser(usageLineLength: lineLength);
                              ^^^^^^^^^^^^^^^
../../../Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/args-1.4.4/lib/src/arg_parser.dart:43:11: Context: Found this candidate, but the arguments don't match.
  factory ArgParser(bool allowTrailingOptions: true) =>
          ^
../../../Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/build_runner-1.11.0/bin/src/commands/generate_build_script.dart:18:31: Error: No named parameter with the name 'usageLineLength'.
  final argParser = ArgParser(usageLineLength: lineLength);
                              ^^^^^^^^^^^^^^^
../../../Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/args-1.4.4/lib/src/arg_parser.dart:43:11: Context: Found this candidate, but the arguments don't match.
  factory ArgParser(bool allowTrailingOptions: true) =>

奇怪的是,所有这些错误都在项目之外。 调用flutter cleanflutter pub cache repair 无效。所以问题是,需要做什么才能让build_runner 继续工作(现在已经过时)Flutter version 1.22.6? 非常欢迎您的建议,谢谢!

【问题讨论】:

【参考方案1】:

由于必需的第三方库阻止迁移到 null 安全,唯一的解决方案是通过从生成的 json 切换到手写的来消除 build_runner。

与其说是解决方案,不如说是一种变通方法,但已经足够了。

【讨论】:

以上是关于如何修复 Flutter build_runner 错误:无法预编译 build_runner:build_runner:的主要内容,如果未能解决你的问题,请参考以下文章

为啥 build_runner 在 dart/flutter 中序列化 JSON 时不生成文件

Flutter:读取 build.yaml 文件中传递给 build_runner 的变量

flutter pub run build_runner build 时出错

flutter-web: build_runner getter 'typeArguments' 没有为类 'Annotation' 错误定义

使用 json_serializable (flutter packages pub run build_runner build) 问题

Flutter build_runner watch 不工作 |在 null 上调用了 getter 'definingUnit'