-
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
Unexpected nullable error for null-checked object. #40077
Comments
Smart cast works for parameters, so it seems specific to fields. |
You can only promote local variables, not instance variables or static variables. So, try adding: var birthday = this.birthday; before the |
Yes, that is the solution which the release note shows. |
The spec can't read such a restriction, but I haven't understood "normal promotion". |
The reason to not promote instance variables or static variables, or any variable where we cannot convince ourselves that there is no assignment to it between the test and the use is ... well, that we can't be sure that the value doesn't change. So if you do: if (foo is Bar) {
flop();
foo.barMethod();
} then we cannot be sure that the It's even possible to make local variable promotion unsafe. void bar() {
num x;
void update(num z) { x = z; };
if (x is int) {
foo(update);
print(x.toRadixString(16));
}
} Here we cannot convince ourselves that the local variable So, promotion only works when we know (based on our compiler's somewhat simplistic analyses) that the value we check is the same value that we later use at the checked type. We are working on improvements for the compiler as part of the non-nullable types feature, so it's not as completely inane as the current promotion, but it still won't be smart enough to figure out whether you assign to a non-local variable or not. |
I understand. |
another discussion |
The following code cause unexpected error.
Based on Dart SDK 2.7.0-edge.134e0e28cda1f8110a69bad34ae70e8123475a80
https://nullsafety.dartpad.dev/d17785edcd0c3cbcedd381890a4ed1e3
The text was updated successfully, but these errors were encountered: