Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get a reference of a type from my element attribute to create an import statement? #502

Open
atreeon opened this issue Feb 8, 2021 · 1 comment

Comments

@atreeon
Copy link

atreeon commented Feb 8, 2021

I am generating code from a function

@mycodegen
List<Cat> getCats() {
  return cats;
}

I need now to get an import statement when I reproduce the call method when generating code in my library builder, otherwise a reference to Cat will be missing. I can do this in the generator:

callMethod.type.returnType.element.location

which produces something like

asset:example/test/Cat.dart;asset:example/test/Cat.dart;Cat

I would then have to do some string manipulation to get the bits I want to create an import statment. Is this best practice or is there an easier way?

@jakemac53
Copy link
Contributor

You can get an AssetId through the assetIdForElement api which would give you a bit more directly what you need.

That has a uri getter, but it will only give an "importable" uri if it can be mapped to a package: uri. If it comes from something outside of a lib directory you would have to construct a relative path to it using the path property of the asset id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants