I'm using Dart's analyzer API, which allows me to introspect Dart code.
Here is some example code:
void soIntense(anything, {bool flag: true, int value}) { }
Notice how the flag
parameter has a default value of true
.
How can I get the default value, assuming I have an instance of ParameterElement
?
Here's the best way that I found. I'm hoping there's a better way.
First, check that there is a default value:
bool hasDefaultValue = _parameter.defaultValueRange != null &&
_parameter.defaultValueRange != SourceRange.EMPTY;
Then, you can use a ParameterElement
's defaultValueRange
.
SourceRange range = _parameter.defaultValueRange;
return _parameter.source.contents.data.substring(range.offset, range.end);
In english:
Get the parameter element's Source's content's data's substring.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With