Issues with build_runner after adding Hive Field

1.3k views Asked by At

I have added addtional Hive Fields to a Hive type before as needed. I have then deleted .g.dart file and regenerated it with no problems.

This time after added a new Hive Field I have this error and I am not able to understand why

[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

I have tried flutter clean, flutter pub outdated, pub get, etc... and I keep getting this error.

In my pubspec.yaml I have, this worked just fine until I have added the latest 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

I found an old post about downgrading analyzer, but this then causes problems with the versions I am using.

In the main.dart there isn't an error on line 33:3. it should not have ; as suggested. Again this worked fine before, so I am not sure why it is an issue now. I also commented out the code here and I still get the same error message even with nothing at line 33:3

If I remove the field I added I still get the same error. If I restore the .g.dart file from a backup I can run the app as before.

Any input on this would be appreciated as it's very confusing

Update:

So I tried deleting the model file and completely recreating it and then generating the .g.dart. I get this:

[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)

I have tried keeping the file as it was, added a new Hive Field to a previous Hive Type and adding a Hive type and I get nothing generated.

I haven't changed the existing Hive fields, I know that causes issues. New Hive Fields and Hive Types were given the next number that hadn't been used before.

2

There are 2 answers

0
rjh500 On BEST ANSWER

So I didn't fix the issue, but for a work around I created a new flutter project, copied my code over except any generated files. Then I was able to generate new type adapters after adding new Hive fields and hive types. Maybe something got corrupted in my previous project. Not sure why flutter clean wouldn't fix this. But maybe this will help someone stuck on the same issue.

0
Sn1cKa On

Set source_gen: 1.0.3 in pubspec.yaml helped in my case.