You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Why is this giving an error? The string is final so it will never be null.
voidmain() {
final test =SomeClass(null);
test.printPassword();
}
classSomeClass{
finalString? password;
SomeClass(this.password);
voidprintPassword(){
if (password ==null) {
print('Password is null');
}else{
print(password +' should never be null');
}
}
}
The text was updated successfully, but these errors were encountered:
vanlooverenkoen
changed the title
Null check required after checking a final field
Null safety feedback: Null check required after checking a final field
Nov 22, 2020
Dart only ever promotes local variables. You can't know for sure that a virtual getter always returns the same value, even if it's a final field. A subclass can override the getter with something else, and that subclass might be declared in a completely different package.
Dart only promotes when it can prove that the value you test is also the same value that you are using later, based only on the signatures. It can only do that for local variables.
Closing since this is a duplicate and there is more in-depth conversation in other issues.
field-promotionIssues related to addressing the lack of field promotion
Why is this giving an error? The string is final so it will never be null.
The text was updated successfully, but these errors were encountered: