-
Notifications
You must be signed in to change notification settings - Fork 210
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
Null safety flow analysis on nullable object fields #1722
Comments
It's a design decision. Dart can only promote local variables. The You can copy the value into a local variable, then it can be promoted: var a = myObj.a;
if (a != null) print(a.length);
} |
What about when class Foo {
final String? a;
Foo(this.a);
void bar() {
if (a != null) {
print(a?.length);
}
}
} it's not a local variable but it's close enough, don't you think? |
The problem is that Your code is equivalent to this: class Foo {
final String? _a;
Foo(this.a);
/// No setter means that [a] is `final`.
String? get a => _a;
void bar() {
if (a != null) print(a?.length);
}
} Which is simple enough. But because getters are just regular functions with no parameters, it can also have arbitrary logic. Consider: import "dart:math";
class Foo {
final String? _a;
Foo(this.a);
/// Sometimes returns [null], sometimes returns [_a].
///
/// In a real program, this wouldn't be random, but instead depend on some state.
String? get a => Random().nextBool() ? _a : null;
/// A more realistic example of a getter that returns different values.
String? get username => MyAuth.isSignedIn ? MyAuth.user.id : null;
void bar() {
if (a != null) { // Just because a isn't null here...
print(a.length); // ...doesn't mean it isn't null here!
}
}
} There are a few workarounds being discussed:
|
I'm interested in the flow analysis of nullable object fields. The problem is that it is not actually working so I'm wondering if this is an issue or a design decision.
Flutter + Dart version:
[✓] Flutter (Channel stable, 2.2.0, on macOS 11.4 20F71 darwin-x64, locale en-GB)
• Flutter version 2.2.0 at /Users/kiko.balaj/Documents/flutter
• Framework revision b22742018b (7 weeks ago), 2021-05-14 19:12:57 -0700
• Engine revision a9d88a4d18
• Dart version 2.13.0
Example
The issue is in the
bar
method. In my opinion, in theprint
argument there doesn't have to be the questionmark operator after themyObj.a
because I've already checkedmyObj.a != null
.Thanks for the response!
The text was updated successfully, but these errors were encountered: