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

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

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

dart reflectable in library

I'm making a library for persistence implementation. For that I want to use reflection to be able to restore an object from the storage.
I can't use dart:mirrors because this library will be use also in Flutter application. So I'm trying to use reflectable package. And here I'm getting a major issue with understanding of how it should work. The doc tells reflectable uses build package and that some files must be generated. But I couldn't find anywhere whether this generation should happen for each file in my library package or just those where reflectable is used.
My library project structure is standard:
/examples
/lib
/lib/src
/lib/my_package.dart
/test
/pubspec.yaml
When I run dart run build_runner or dart run build_runner lib from the project root I see for each file in /example and /tests a matching file is generated. But in /lib folder nothing is generated. Only file where I'm intending to use reflectable is /lib/src/persistence/persistence_model.dart. I've created a file /lib/build.yaml of following content:
targets:
$default:
builders:
reflectable:
generate_for:
- src/persistence/persistence_model.dart
options:
formatted: true
but it seems to have no effect.
So, what should be right approach?
And probably a side question or rather consideration: isn't using reflection in Dart and Flutter excessively complex comparing to other languages (Python, C#, Java)?

ERROR:: Unknown Directory structure, both java & kotlin files not found

As I was trying to change my package name using a package (change_app_package_name). I bumped into this error:-
Old Package Name: com.milanzi.shamba_huru
Updating build.gradle File
Updating Main Manifest file
Updating Debug Manifest file
Updating Profile Manifest file
ERROR:: Unknown Directory structure, both java & kotlin files not found.
Seems the package cannot find either java or kotlin files within the directory.
I have solved by changing the directory name where the MainActivity.kt is placed
For example
First your package name is com.example.app_name
Then you need to change into com.milanzi.shamba_huru (use your own package name)
Run
flutter pub run change_app_package_name:main com.milanzi.shamba_huru
So if the error appears, you just need to create a new folder by name of your package
Before your path was
/android/app/src/main/kotlin/com/example/app_name/MainActivity.kt
Change into
/android/app/src/main/kotlin/com/milanzi/shamba_huru/MainActivity.kt
Now try run against flutter pub run change_app_package_name:main com.milanzi.shamba_huru
NB: If you didn't change the folder name, you can't run it on android. But you can run on IOS.

Couldn't Import a dart file in a new dart file (Target of URI doesn't exist)

My flutter project has a separate dart file for each functionality, So, I tried to import a dart file in a new dart file to access the functionality in that file, but I got this error "Target of URI doesn't exist: 'package:filename.dart'. Try creating the file referenced by the URI, or Try using a URI for a file that does exist"
I tried solutions like restarting the Ide, flutter clean command and pub get, Most of the solutions for the same error have to deal with packages, it can be solved by adding package name in dependencies. But this is a dart file, how do I solve it.
When using the import function and going to the root folder with 'package:*', you need to first write the flutter project folder name, and then after that the file or subfolders.
All this but skipping the 'lib' folder.
As an example:
import 'package:flutterProject/subFolderInLib/filename.dart'
Hope this was understandable and worked! :)

Target URI doesn't exist, when trying to import a package in flutter

I have stumbled upon a problem in importing the package in Flutter, I tried to solve this by running flutter packages get and also shutting down the project in Android studio and reopening it.
import 'package:task_02_category_widget/category.dart';
Here is the line above, and the error I'm running into when I run it gives the following error in the console.
Your application could not be compiled, because its dependencies could
not be established.
The following Dart file:
/Users/username/Documents/flutter_rectangle_2/lib/main.dart
...refers, in an import, to the following library:
package:task_02_category_widget/category.dart
That library is in a package that is not known. Maybe you forgot to
mention it in your pubspec.yaml file?
If task_02_category_widget/category.dart is part of an old project you are reusing you should put it in a folder in your flutter application and include it like "../ folder /task_02_category_widget/category.dart ". If it is part of github repository you have copy pasted from, just copy the file and use the step above. Most probably you are looking for that . In any other case check here to find the source code.
You should have in your project at a file called pubspec.yaml a definition like this:
name: my_app
dependencies:
task_02_category_widget:
Let’s say that your package is laid out as follows:
task_02_category_widget/
lib/
category.dart
Then, you can import it:
import 'package:task_02_category_widget/category.dart';
More information:
https://www.dartlang.org/tools/pub/get-started
https://www.dartlang.org/guides/libraries/create-library-packages

golang using functions of imported subdirectories

I can't use functions of custom subdirectories.
My Code Organziation
I have under "src" a path hierarchy like
a/b
with all my directories and go-Files (it is the "root" of my project). The directories contain no subdirectories and it works fine. So the deepest path is "a/b/c". E.g. I have
a/b/c
and
a/b/d
with some go-files. Import of "a/b/d" and calling a function with "d.DoSomething()" from a file in "a/b/c" works fine.
Problem description
Now I want ot reorganize "a/b/d". I move some files from "a/b/d" to
a/b/d/e
and the rest of the files to
a/b/d/f
If try to import "a/b/d/e" with import-statement
import ( "a/b/d/e" )
from the same file in "/a/b/c" and want to call "e.DoSomething()" (it is the place, where the file with the "DoSomething-function" moved to), I get an error at the line, where I call "e.DoSomething()": "undefined: e".
While searching for a result, I've nowhere seen examples with deeper path hierarchies. Is it generally not possible to use/import subdirectories or what's the problem?
go-version I used: go1.2.2 linux/amd64
Thanks for any advices
Your approach is completely wrong. Go has absolutely no concept of importing files or directories, all you can import in Go are packages. It now happens that the name of a package is it's path relative to GOPATH and you import packages by that name. But the identifier under which an imported package is available in the importing code depends on the package declaration of the package. You cannot simply "move" files between directories as each directory (for the go tool) is a single package without changing the package declaration.
You can have package x under path a/b/c. When you import package x with import ( "a/b/c" ) all the exported stuff from package x is available to you as x.ExportedName.
Please read http://blog.golang.org/organizing-go-code carefully.
Try and do a go build in a/b/d/e first, before trying to build in a/b: that will generate the compiled classes you want to import.