Dart analyzer: get the type of the initial value of a field

How do I get the type of the original value expression using the Dart analyzer API?

class MyClass {
  var prop = <initial value expression>;
}

      

If the initial value expression is, for example 'text'

, I would like to get String

. If this is a function call, I would like to get the return type of the function.

+3


source to share


1 answer


Once you have a fully resolved AST structure, ask Expression

for an expression representing its initial value staticType

. This will return DartType

representing the static type.



For type inference, it is possible to create a more specialized type that can be accessed with propagatedType

. (And if you don't care which type you get, you can use bestType

.

+5


source







All Articles