sdk
sdk copied to clipboard
Encapsulate field assist miss-types the constructor field if the type is from an aliased import
Repro:
type.dart
class MyType {}
main.dart
import 'type.dart' as type;
class C {
C({required this.t});
type.MyType t;
}
Now use the "encapsulate field" quick-fix on t. It will generate a getter and setter of the correct type (with alias) but not the constructor.
Result:
import 'type.dart' as type;
class C {
C({required MyType t}) : _t = t; // <-- "MyType" here throws `undefined_class`
type.MyType _t;
type.MyType get t => _t;
set t(type.MyType value) {
_t = value;
}
}
As stated in the above comment, this sample shows an error for undefined_class, but this could be a problem with classes with the same name (aliases usually are added for that reason) that are related in some way so this would not show any errors now only when attributing the values later.
Summary: The "encapsulate field" quick-fix incorrectly uses the unqualified type name in the constructor parameter when the field type is from an aliased import. This leads to a compile-time error if the unqualified type name is not defined in the current scope.
@fshcheglov This is related to https://github.com/dart-lang/sdk/issues/56559, you should be able to solve it.
https://dart-review.googlesource.com/c/sdk/+/385322