non_constant_default_value
Details about the 'non_constant_default_value' diagnostic produced by the Dart analyzer.
The default value of an optional parameter must be constant.
Description
#The analyzer produces this diagnostic when an optional parameter, either named or positional, has a default value that isn't a compile-time constant.
Example
#The following code produces this diagnostic:
var defaultValue = 3;
void f([int value = defaultValue]) {}
Common fixes
#If the default value can be converted to be a constant, then convert it:
const defaultValue = 3;
void f([int value = defaultValue]) {}
If the default value needs to change over time, then apply the default value inside the function:
var defaultValue = 3;
void f([int? value]) {
value ??= defaultValue;
}
除非另有说明,文档之所提及适用于 Dart 3.10.3 版本报告页面问题.