You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行build_runner命令报错:ClassDeclaration无macroKeyword getter

build_runner构建失败:macroKeyword未定义错误

错误详情

执行build_runner命令时出现以下构建错误:

Failed to build build_runner:build_runner:
../../.pub-cache/hosted/pub.dev/dart_style-2.3.5/lib/src/front_end/ast_node_visitor.dart:251:16: Error: The getter 'macroKeyword' isn't defined for the class 'ClassDeclaration'.
  - 'ClassDeclaration' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../.pub-cache/hosted/pub.dev/analyzer-6.2.0/lib/src/dart/ast/ast.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'macroKeyword'.
          node.macroKeyword,
                ^^^^^^^^^^^^
../../.pub-cache/hosted/pub.dev/dart_style-2.3.5/lib/src/source_visitor.dart:595:19: Error: The getter 'macroKeyword' isn't defined for the class 'ClassDeclaration'.
  - 'ClassDeclaration' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../.pub-cache/hosted/pub.dev/analyzer-6.2.0/lib/src/dart/ast/ast.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'macroKeyword'.
    modifier(node.macroKeyword);
                  ^^^^^^^^^^^^

问题原因

dart_style 2.3.5版本与analyzer 6.2.0版本不兼容:dart_style代码中引用了ClassDeclaration类的macroKeyword属性,但analyzer 6.x版本已经移除了该属性,导致构建时找不到对应 getter。

解决步骤

  • 更新dart_style到兼容版本:在项目的pubspec.yaml中指定dart_style的版本至少为2.3.6(该版本修复了与analyzer 6.x的兼容问题),示例:
    dev_dependencies:
      dart_style: ^2.3.6
    
  • 清理缓存并重新拉取依赖:
    dart pub cache clean
    dart pub get
    
  • 重新运行build_runner:
    dart run build_runner build
    

内容的提问来源于stack exchange,提问作者Code Hunter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 23:29:55