-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect error message when initializer uses setter #39097
Comments
@lrhn for the language interpretation |
tl;dr: You can't use the instance setter during object initialization because an instance member needs a value for The error message is actually correct. You are trying to initialize "text" in the initializer list, you can only initialize fields (instance variables) declared in the same class, so it's reasonable to say that " The initializer does not try to access a setter which is there ... because an initializer never accesses a setter, it can only assign an initial value directly to a field (or rather, directly to the underlying storage that will become the field when the object is complete). The Dart object model enforces a strict no-access policy for uninitialized objects. An object is only accessible after it has been fully initialized, which is why you can never see a final fields before its value is set. The way an object is initialized is that any final field declared by a class must be initialized either
A non-final field can (for now) omit being initialized, in which case it's automatically initialized to Until all fields have been initialized, nobody can get a reference to the object being created, you cannot use A Dart field is really an anonymous storage slot on the object along with three ways to access it:
Since the setter and getter are instance members, they cannot be used while initializing the object. Until all of the initialization have completed, only field initialization is available, and afterwards, only getters/setters are available. So, for this example:
|
@irhn, Thank you for the awesome detailed explanation. My error was seeing "field" but reading "member." |
When an initializer attempts to use a setter that is present, I get the following error:
This error occurs for both
text
andauthor
in the following:Aside from correcting the error message, I sure would like to know why this isn't allowed. It was explicitly disallowed very early in issue #92. I'm having to add static methods that both the initializers and the setters call. It would be so much easier to just leverage the setter.
The text was updated successfully, but these errors were encountered: