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

Darttype.element as ClassElement, but element2 was prompted after the upgrade #374

Closed
xiaocode337317439 opened this issue Aug 11, 2022 · 6 comments

Comments

@xiaocode337317439
Copy link

xiaocode337317439 commented Aug 11, 2022

Darttype. element as ClassElement
Not sure how to convert to ClassElement after using element2 ?

        final ele = type.element as ClassElement; // type => DartType
        // final ele = type as InterfaceType;
        final toJson = ele.lookUpMethod('toJson', ele.library);
        // final toJson = ele.getMethod("toJson");
        if (toJson == null) {
          value = refer("jsonEncode($displayName)");
        } else {
          value = refer("$displayName?.toJson()");
        }
@srawlins
Copy link
Member

CC @scheglov

@scheglov
Copy link

InterfaceType.element2 is InterfaceElement, and will be in future version not only ClassElement, but also EnumElement, or MixinElement. Right now both are also ClassElement, but we will separate them in the next breaking changes version. I think you might use InterfaceElement methods, without casting to ClassElement.

@xiaocode337317439
Copy link
Author

InterfaceType.element2 is InterfaceElement, and will be in future version not only ClassElement, but also EnumElement, or MixinElement. Right now both are also ClassElement, but we will separate them in the next breaking changes version. I think you might use InterfaceElement methods, without casting to ClassElement.

How should DartType be converted to InterfaceType?

@xiaocode337317439
Copy link
Author

InterfaceType.element2 is InterfaceElement, and will be in future version not only ClassElement, but also EnumElement, or MixinElement. Right now both are also ClassElement, but we will separate them in the next breaking changes version. I think you might use InterfaceElement methods, without casting to ClassElement.

How should DartType be converted to InterfaceType?

TypeChecker(List).isExactly((ParameterElement.type as InterfaceElement))

error => type 'ParameterElementImpl' is not a subtype of type 'InterfaceElement' in type cast

@scheglov
Copy link

You don't convert, you check if it is InterfaceType, using if (myType is InterfaceType) {...}. It might be something different, e.g. FunctionType.

@xiaocode337317439
Copy link
Author

You don't convert, you check if it is InterfaceType, using if (myType is InterfaceType) {...}. It might be something different, e.g. FunctionType.

Thank you for answering

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

3 participants