Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart analyzer: Get type of initial value of a field

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.

like image 445
maiermic Avatar asked Sep 06 '25 12:09

maiermic


1 Answers

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.

like image 81
Brian Wilkerson Avatar answered Sep 10 '25 18:09

Brian Wilkerson