-
Notifications
You must be signed in to change notification settings - Fork 1.7k
analyzer complains even when checking for null values #44463
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
Comments
The reason that a test for null can sometimes cause a later use of a nullable variable to be allowed, is that it causes a type promotion from the nullable type to the non-nullable (sub-)type. This is the same as any other type promotion, say having a Dart only performs type promotion when it's guaranteed to be safe, when the compiler is absolutely certain that the value you check is also the value you use later. You can copy the variable to a local variable: void f() {
var s = this.s;
if (s != null && s.length > 3) { /*...*/ }
} or you can add a void f() {
if (s != null && s!.length > 3) { /*...*/ }
} |
Could the analyzer give a different error message when something looks like the user wanted it to promote, but it doesn't for non-local variables. |
@stereotype441, would it be overkill to let the analyzer maintain the same information about non-local variables (that is, instance and static variables of the enclosing class and top-level variables) as the information which is currently maintained for local variables, and then check for each failing member lookup whether the member is statically known based on the would-be promoted type? We could then report that the failure could be caused by a lack of promotion. |
Follow #38773 for the resolution to this issue. |
I get an error even when checking for null value:
An expression whose value can be 'null' must be null-checked before it can be dereferenced at
The text was updated successfully, but these errors were encountered: