2

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

  • This sounds like you configured the output directory? Or the generated files are in some subfolder? Then you need to adjust the objectbox.g.dart import from the example accordingly (e.g. add as many `../` as necessary). Also here is one of our official examples, if helpful: https://github.com/objectbox/objectbox-dart/tree/main/objectbox/example/flutter/objectbox_demo_relations – Uwe - ObjectBox Jun 27 '22 at 06:28

1 Answers1

0

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
ubiquibacon
  • 10,451
  • 28
  • 109
  • 179
  • Where in pubspec.yaml are you putting that code? Are you still putting in the objectbox_flutter_libs and objectbox_generator in dev dependancies? I tried just under the objectbox call, in dev dependancies and as a dependancy override but nothing wanted to work – KatieK Oct 15 '22 at 14:49
  • The code snippet in my answer goes at the "top level" of the `pubspec.yaml`. I.e. `objectbox:` is a sibling to `dependencies:`, `dev_dependencies:`, etc. – ubiquibacon Oct 16 '22 at 05:39