sdk
sdk copied to clipboard
Analyzer: dot-shorthand fails when constructor and member have same name
When a named constructor and a non-static member (field, getter, method) have the same name, the analyzer and IDE plugins will report an error, even though the compiler seems to accept it.
Example code:
class Example {
Example.value(this.value);
final int value;
}
void printExample(Example example) {
print('Value: ${example.value}');
}
void main() {
printExample(.value(10));
// ~~~~~~ ERROR: The static method or constructor 'value' isn't defined for the context type 'Example'.
}
dart analyze output:
Analyzing dart-test... 0.4s
error • test.dart:12:17 • The static method or constructor 'value' isn't
defined for the context type 'Example'. Try correcting the name to
the name of an existing static method or constructor, or defining a
static method or constructor named 'value'. •
dot_shorthand_undefined_member
1 issue found.
dart run and dart compile exe both produce the correct results.
An explicit Example.value(10) produces no errors or warnings.
Tested with:
$ dart --version
Dart SDK version: 3.10.1 (stable) (Tue Nov 18 11:02:31 2025 -0800) on "macos_arm64"