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

Flutter 3.19.2执行build_runner build失败,求原因及修复方案

问题:执行flutter pub run build_runner build时出现dart_style相关错误

我在执行flutter pub run build_runner build命令时遇到了如下错误,此前仅修改了某个远程响应类中的@JsonKey(name: "key")配置,这是我首次运行build_runner时出现该问题。使用的Flutter版本为3.19.2。

错误信息

Failed to build build_runner:build_runner:
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/source_visitor.dart:1287:38: Error: Type 'ExtensionTypeDeclaration' not found.
  void visitExtensionTypeDeclaration(ExtensionTypeDeclaration node) {
                                     ^^^^^^^^^^^^^^^^^^^^^^^^
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/source_visitor.dart:2629:43: Error: Type 'RepresentationConstructorName' not found.
  void visitRepresentationConstructorName(RepresentationConstructorName node) {
                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/source_visitor.dart:2635:39: Error: Type 'RepresentationDeclaration' not found.
  void visitRepresentationDeclaration(RepresentationDeclaration node) {
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/source_visitor.dart:1287:38: Error: 'ExtensionTypeDeclaration' isn't a type.
  void visitExtensionTypeDeclaration(ExtensionTypeDeclaration node) {
                                     ^^^^^^^^^^^^^^^^^^^^^^^^
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/source_visitor.dart:2629:43: Error: 'RepresentationConstructorName' isn't a type.
  void visitRepresentationConstructorName(RepresentationConstructorName node) {
                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/source_visitor.dart:2635:39: Error: 'RepresentationDeclaration' isn't a type.
  void visitRepresentationDeclaration(RepresentationDeclaration node) {
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/front_end/ast_node_visitor.dart:623:39: Error: Type 'ExtensionTypeDeclaration' not found.
  Piece visitExtensionTypeDeclaration(ExtensionTypeDeclaration node) {
                                      ^^^^^^^^^^^^^^^^^^^^^^^^
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/front_end/ast_node_visitor.dart:1543:44: Error: Type 'RepresentationConstructorName' not found.
  Piece visitRepresentationConstructorName(RepresentationConstructorName node) {
                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/front_end/ast_node_visitor.dart:1551:40: Error: Type 'RepresentationDeclaration' not found.
  Piece visitRepresentationDeclaration(RepresentationDeclaration node) {
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/front_end/ast_node_visitor.dart:623:39: Error: 'ExtensionTypeDeclaration' isn't a type.
  Piece visitExtensionTypeDeclaration(ExtensionTypeDeclaration node) {
                                      ^^^^^^^^^^^^^^^^^^^^^^^^
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/front_end/ast_node_visitor.dart:629:49: Warning: The null-check pattern will have no effect because the matched type isn't nullable.
Try replacing the null-check pattern with its nested pattern.
          if (node.constKeyword case var keyword?) keyword
                                                ^
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/front_end/ast_node_visitor.dart:1543:44: Error: 'RepresentationConstructorName' isn't a type.
  Piece visitRepresentationConstructorName(RepresentationConstructorName node) {
                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/front_end/ast_node_visitor.dart:1551:40: Error: 'RepresentationDeclaration' isn't a type.
  Piece visitRepresentationDeclaration(RepresentationDeclaration node) {
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/front_end/piece_factory.dart:892:7: Error: Type 'RepresentationDeclaration' not found.
      RepresentationDeclaration? representation,
      ^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/front_end/piece_factory.dart:386:46: Error: Non-nullable variable 'forPartsPiece' must be assigned before it can be used.
    var forPiece = ForPiece(forKeywordPiece, forPartsPiece, bodyPiece,
                                             ^^^^^^^^^^^^^
../../../../.pub-cache/hosted/pub.dev/dart_style-2.3.6/lib/src/front_end/piece_factory.dart:892:7: Error: 'RepresentationDeclaration' isn't a type.
      RepresentationDeclaration? representation,
      ^^^^^^^^^^^^^^^^^^^^^^^^^

错误原因

核心问题是dart_style 2.3.6版本与Flutter 3.19.2对应的Dart SDK版本不兼容:

  • dart_style 2.3.6中使用了Dart 3.0+新增的ExtensionTypeDeclaration等语法类型,但Flutter 3.19.2配套的Dart SDK(3.3.0版本左右)对这些类型的定义存在差异,导致dart_style无法找到对应类型。
  • 修改@JsonKey的操作只是触发了build_runner执行,本身不会引发该错误,只是暴露了潜在的依赖版本冲突。

修复方法

方法一:锁定兼容的dart_style版本

在项目的pubspec.yaml中指定与Flutter 3.19.2兼容的dart_style版本:

dev_dependencies:
  dart_style: ^2.3.3  # 该版本适配Dart 3.3.0 SDK

执行flutter pub get更新依赖后,再重新运行flutter pub run build_runner build。

方法二:清理缓存并重新安装依赖

  1. 执行命令清理本地pub缓存:
flutter pub cache clean
  1. 删除项目根目录下的pubspec.lock文件
  2. 重新拉取所有依赖:
flutter pub get
  1. 再次尝试运行build_runner命令

方法三:更新build_runner相关依赖

确保build_runner及其关联的代码生成依赖为兼容版本,更新pubspec.yaml:

dev_dependencies:
  build_runner: ^2.4.8
  json_serializable: ^6.7.1

执行flutter pub get后重新运行build_runner命令。

内容的提问来源于stack exchange,提问作者Naufal Rajabi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 12:54:57