Issue
How can I get the type of an initial value expression of a field using Dart‘s analyzer API?
class MyClass {
var prop = <initial value expression>;
}
If initial value expression is for example 'text'
, I’d like to get String
. If it is a function call, I’d like to get the return type of the function.
Solution
After getting a fully resolved AST structure, ask the Expression
representing the initial value expression for it’s staticType
. That will return the DartType
representing the static type.
It’s possible for type inference to produce a more specialized type, which you can access using propagatedType
. (And if you don’t care which type you get, you can use bestType
.
Answered By – Brian Wilkerson
Answer Checked By – Mary Flores (FlutterFixes Volunteer)