How to get the default value for an optional parameter using the Dart analyzer API?

I use the Dart analyzer API, which allows me to look into the Dart code.

Here is a sample code:

void soIntense(anything, {bool flag: true, int value}) { } 

Note that the flag parameter has a default value of true .

How to get the default value if I have an instance of ParameterElement ?

+5
source share
1 answer

Here is the best way I've found. I hope there will be a better way.

First check if there is a default value:

 bool hasDefaultValue = _parameter.defaultValueRange != null && _parameter.defaultValueRange != SourceRange.EMPTY; 

Then you can use ParameterElement defaultValueRange .

 SourceRange range = _parameter.defaultValueRange; return _parameter.source.contents.data.substring(range.offset, range.end); 

In English:

Get the data source item of the source content.

+4
source

Source: https://habr.com/ru/post/1212913/


All Articles