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

Flutter执行build_runner build时Objectbox生成器报错(错误码78)

解决ObjectBox生成代码时DartType.element不存在的报错

问题场景

开发首个Flutter应用,使用ObjectBox定义数据模型后,执行flutter pub run build_runner build生成代码时,触发DartType类不存在element getter属性的报错。已执行flutter pub upgrade升级依赖,但问题未解决。

报错日志

[INFO] Generating build script...
[INFO] Generating build script completed, took 484ms

[INFO] Precompiling build script......
[WARNING] ../../../../.pub-cache/hosted/pub.dev/objectbox_generator-2.0.0/lib/src/entity_resolver.dart:67:41: Error: The getter 'element' isn't defined for the class 'DartType'.
 - 'DartType' is from 'package:analyzer/dart/element/type.dart' ('../../../../.pub-cache/hosted/pub.dev/analyzer-5.1.0/lib/dart/element/type.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'element'.
            : entityRealClass.typeValue.element!.name!,
                                        ^^^^^^^
../../../../.pub-cache/hosted/pub.dev/objectbox_generator-2.0.0/lib/src/entity_resolver.dart:158:64: Error: The getter 'element' isn't defined for the class 'DartType'.
 - 'DartType' is from 'package:analyzer/dart/element/type.dart' ('../../../../.pub-cache/hosted/pub.dev/analyzer-5.1.0/lib/dart/element/type.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'element'.
        } else if (['Int8List', 'Uint8List'].contains(dartType.element!.name)) {
                                                               ^^^^^^^
../../../../.pub-cache/hosted/pub.dev/objectbox_generator-2.0.0/lib/src/entity_resolver.dart:160:29: Error: The getter 'element' isn't defined for the class 'DartType'.
 - 'DartType' is from 'package:analyzer/dart/element/type.dart' ('../../../../.pub-cache/hosted/pub.dev/analyzer-5.1.0/lib/dart/element/type.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'element'.
        } else if (dartType.element!.name == 'DateTime') {
                            ^^^^^^^
../../../../.pub-cache/hosted/pub.dev/objectbox_generator-2.0.0/lib/src/entity_resolver.dart:186:60: Error: The getter 'element' isn't defined for the class 'DartType'.
 - 'DartType' is from 'package:analyzer/dart/element/type.dart' ('../../../../.pub-cache/hosted/pub.dev/analyzer-5.1.0/lib/dart/element/type.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'element'.
            (f.type as ParameterizedType).typeArguments[0].element!.name;
                                                           ^^^^^^^
../../../../.pub-cache/hosted/pub.dev/objectbox_generator-2.0.0/lib/src/entity_resolver.dart:233:20: Error: The getter 'element' isn't defined for the class 'DartType'.
 - 'DartType' is from 'package:analyzer/dart/element/type.dart' ('../../../../.pub-cache/hosted/pub.dev/analyzer-5.1.0/lib/dart/element/type.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'element'.
            f.type.element!.name! + (isNullable(f.type) ? '?' : '');
                   ^^^^^^^
../../../../.pub-cache/hosted/pub.dev/objectbox_generator-2.0.0/lib/src/entity_resolver.dart:416:12: Error: The getter 'element' isn't defined for the class 'InterfaceType'.
 - 'InterfaceType' is from 'package:analyzer/dart/element/type.dart' ('../../../../.pub-cache/hosted/pub.dev/analyzer-5.1.0/lib/dart/element/type.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'element'.
          .element
           ^^^^^^^
../../../../.pub-cache/hosted/pub.dev/objectbox_generator-2.0.0/lib/src/entity_resolver.dart:449:55: Error: The getter 'element' isn't defined for the class 'DartType'.
 - 'DartType' is from 'package:analyzer/dart/element/type.dart' ('../../../../.pub-cache/hosted/pub.dev/analyzer-5.1.0/lib/dart/element/type.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'element'.
  bool isToOneRelationField(FieldElement f) => f.type.element!.name == 'ToOne';
                                                      ^^^^^^^
../../../../.pub-cache/hosted/pub.dev/objectbox_generator-2.0.0/lib/src/entity_resolver.dart:452:14: Error: The getter 'element' isn't defined for the class 'DartType'.
 - 'DartType' is from 'package:analyzer/dart/element/type.dart' ('../../../../.pub-cache/hosted/pub.dev/analyzer-5.1.0/lib/dart/element/type.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'element'.
      f.type.element!.name == 'ToMany';
             ^^^^^^^
[INFO] Precompiling build script... completed, took 1.5s

[SEVERE] Failed to precompile build script .dart_tool/build/entrypoint/build.dart.
This is likely caused by a misconfigured builder definition.

pub finished with exit code 78

补充代码

数据模型文件(models.dart)

import 'package:objectbox/objectbox.dart';
import 'helper.dart';

@Entity()
class Category {
  int id = 0;

  @Index()
  String? name;

  @Backlink('category')
  var tasks = ToMany<Task>();
}

@Entity()
class Task {
  @Id()
  int id = 0;

  String? name;

  @Transient()
  TaskState state = TaskState.newly;

  int get dbState {
    return state.index;
  }

  set dbState(int val) {
    state = TaskState.values[val];
  }

  var category = ToOne<Category>();
}

@Entity()
class Day {
  int id = 0;

  @Index()
  String date = '';

  String gratitudes = '###';
  String journal = '';

  var tasks = ToMany<Task>();
}

pubspec.yaml配置

name: quick_planner
description: A new Flutter project.
publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: '>=2.19.4 <3.0.0'

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  format: ^1.2.0
  system_theme: ^2.1.0
  objectbox: ^2.0.0
  objectbox_flutter_libs: any

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^2.0.0
  build_runner: ^2.3.3
  objectbox_generator: any

flutter:
  uses-material-design: true

  fonts:
    - family: comfortaa
      fonts:
        - asset: fonts/comfortaa.ttf

解决步骤

  1. 锁定ObjectBox依赖版本:旧版本objectbox_generator:2.0.0不兼容analyzer:5.1.0的API变更(DartType.element被移除),需要升级到适配新版本analyzer的ObjectBox版本。修改pubspec.yaml中的依赖:
    dependencies:
      # ...其他依赖不变
      objectbox: ^2.2.0
      objectbox_flutter_libs: ^2.2.0
    
    dev_dependencies:
      # ...其他依赖不变
      objectbox_generator: ^2.2.0
    
  2. 更新依赖:执行命令:
    flutter pub get
    
  3. 清理构建缓存:执行命令:
    flutter pub run build_runner clean
    
  4. 重新生成代码:执行命令:
    flutter pub run build_runner build
    

原因说明

analyzer 5.x版本对DartType类的API做了变更,移除了element getter属性;而旧版objectbox_generator:2.0.0仍在使用这个旧API,导致编译报错。升级到objectbox_generator:2.1.0+版本后,已经适配了analyzer 5.x的新API,问题即可解决。

内容的提问来源于stack exchange,提问作者Forever Spring

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 11:48:11