ObjectBox generator warning:"Failed to find package root from output directory, generated imports might be incorrect."

Viewed 143

Iam using the latest version of objectbox and when i run the "flutter pub run build_runner build" command i get this output: "...Failed to find package root from output directory, generated imports might be incorrect..."

So when i write in main to init ObjectBox:

late ObjectBox objectBox;

ObjectBox is not recognized with import. Using the import from an tutorial which have the same version does not solve the problem,but the json and g.dart files are generated.

What iam doing wrong? Let me know if you need more information! Thanks

1 Answers

You will get the error you are seeing if your ObjectBox files are in the wrong directory. By default, ObjectBox expects objectbox.dart, objectbox.g.dart, and objectbox-model.json to be at the root of your project (i.e. in the lib directory next to the main.dart file. You can move objectbox.dart though. I typically put it in lib/objectbox, but I think that objectbox.dart and the generated files need to exist in the same directory, though I'm not 100% sure about that. Change the directory where objectbox.g.dart and objectbox-model.json are generated by following the instructions below from the ObjectBox README.md. If you wanted to put the files in lib/objectbox you would just change custom to objectbox in the code snippet below:

To customize the directory (relative to the package root) where the generated files are written, add the following to your pubspec.yaml:

objectbox:
  # Writes objectbox-model.json and objectbox.g.dart to lib/custom (and test/custom).
  output_dir: custom
  # Or optionally specify the lib and test output folder separately.
  # output_dir:
  #   lib: custom
  #   test: other
Related