-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[NNBD] Analyser missing explicit null check for class fields #45265
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
Another use case, that is also coming from this same limitation:
Does not work, I have to write either:
Or
Or
The early exit is quite a bit nicer to read and to write, especially if the method body is long and references controller many times. For example, this will look rather gross full of ? and ??, when all I really want to do is say:
In an ideal world, none of this would be necessary, as it's a private method, and in my build routine I have something like:
So compiler should actually know that this method can only be called when controller is non-null, and really leave all my private methods alone. |
I can make some sort of workaround for this using a closure, but it's not great:
Then I can do:
|
Fields are not promotable elements. Subclasses may contain getters which return different objects on each access. @stereotype441 do we have a link where we can point folks to which describes why the Dart flow analysis does not promote fields? |
I believe either @munificent or @kwalrath was planning to write something up? |
There's a mention here: https://dart.dev/guides/language/language-tour#late-variables I don't know whether we've published more detail elsewhere, but if we have, I'd be happy to link to it. The thing is, though... how would people find this doc? Can the tool point them to the explanation, somehow? |
Oh, I didn't realize that something like this would actually compile:
It's really quite odd, I can "override" a This is a rather large pain point in flutter, all kinds of common use cases are made cludgier:
Using ! creates some gaping holes in the compiler, because now the null check could be removed without any error, which we wouldn't want. So really the only way we can write this soundly is:
The real kicker is this would be used 99% of the time in Flutter with Stateless or Stateful Widget, which are, in practice, virtually never overridden in the way this would require. I wonder if there is some sort of quality of life annotation that could exist here, so StatefulWidget and StatelessWidget can be exempted from this aggressive checking, allowing us to not fill our layouts with this rather redundant code. |
@leafpetersen also has great links in a comment he made at dart-lang/language#1494 (comment):
|
Why does the compiler complain here? Compiler should know that this is a
final
field? This pattern is used very often in flutter, where we will check a property of the widget to see if it's null.I think I understand this in general, because we can't be sure some subsequent function does not change a field. But, in this case its in the immediately following line. With something like
if(value == null) return;
how can there possibly be an issue? Additionally many times these fields arefinal
so they actually can not be changed.To work around this, we need to add local version of each field:
The text was updated successfully, but these errors were encountered: