添加 Hive 字段后 build_runner 的问题

Issues with build_runner after adding Hive Field

我之前根据需要向 Hive 类型添加了额外的 Hive 字段。然后我删除了 .g.dart 文件并重新生成它没有问题。

这次在添加了一个新的 Hive Field 之后出现了这个错误,我无法理解为什么

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

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 73ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 968ms

[INFO] Running build...
[INFO] Running build completed, took 21ms

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 55ms

[SEVERE] hive_generator:hive_generator on lib/main.dart (cached):

This builder requires Dart inputs without syntax errors.
However, package:flutter_app_hive/main.dart (or an existing part) contains the following errors.
main.dart:33:3: Expected to find ';'.
main.dart:33:3: Expected an identifier.

Try fixing the errors and re-running the build.

[SEVERE] Failed after 92ms
pub finished with exit code 1

我已经尝试过 flutter clean、flutter pub outdated、pub get 等...但我一直收到此错误。

在我的 pubspec.yaml 中,在我添加最新的 Hive Field 之前,它工作得很好。

dependencies:
  flutter:
    sdk: flutter
  hive: ^2.0.2
  hive_flutter: ^1.0.0
  path_provider: ^2.0.1
  curved_navigation_bar: ^0.3.7
  video_player: ^1.0.1
  font_awesome_flutter: ^8.5.0
  badges: ^1.2.0
  cupertino_icons: ^1.0.2

dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner: ^2.0.1
  hive_generator: ^1.1.0

我发现了一个关于降级分析器的旧post,但这会导致我使用的版本出现问题。

在 main.dart 中,第 33:3 行没有错误。它不应该有;按照建议。同样,这之前工作得很好,所以我不确定为什么现在它是一个问题。我也在这里注释掉了代码,即使 33:3

行没有任何内容,我仍然收到相同的错误消息

如果我删除我添加的字段,我仍然会遇到同样的错误。如果我从备份中恢复 .g.dart 文件,我可以像以前一样 运行 应用程序。

如有任何意见,我们将不胜感激,因为它非常令人困惑

更新:

所以我尝试删除模型文件并完全重新创建它,然后生成 .g.dart。我明白了:

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

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 83ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 1.1s

[INFO] Running build...
[INFO] Running build completed, took 23ms

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 66ms

[INFO] Succeeded after 102ms with 0 outputs (0 actions)

我已尝试保持文件原样,将新的 Hive 字段添加到以前的 Hive 类型并添加 Hive 类型,但我没有生成任何内容。

我没有更改现有的 Hive 字段,我知道这会导致问题。新的 Hive 字段和 Hive 类型被赋予了以前没有使用过的下一个数字。

所以我没有解决这个问题,但为了解决这个问题,我创建了一个新的 flutter 项目,复制了我的代码,除了任何生成的文件。然后我能够在添加新的 Hive 字段和配置单元类型后生成新的类型适配器。也许我以前的项目中有什么东西被破坏了。不知道为什么 flutter clean 不能解决这个问题。但也许这会帮助遇到同样问题的人。

在 pubspec.yaml 中设置 source_gen: 1.0.3 对我有帮助。