Description
@mit-mit points out that the errors that occur when the user attempts to promote a property could be clearer. For example, this code:
class C {
int? x;
test() {
if (x != null) {
print(x.isEven);
}
}
}
Produces the error message:
The property 'isEven' can't be unconditionally accessed because the receiver can be 'null'.
with the following suggested fix:
Try making the access conditional (using '?.') or adding a null check to the target ('!').
and then the following context message (pointing to the declaration int? x;
):
'x' refers to a property so it couldn't be promoted. See http://dart.dev/go/non-promo-property
Two things are unfortunate about this:
(1) The user has to read through a lot of error message text before they get to the meat of the problem (properties can't be promoted).
(2) The suggested fix isn't great.
It would be better to have an error message like this:
Cannot access 'isEven' because 'x' can be null. Because 'x' is a property, it cannot be promoted to non-null. See http://dart.dev/go/non-promo-property
and a suggested fix like this:
Try assigning 'x' to a local variable, for example: final x = this.x;
It probably would still be useful to have a context message pointing to the declaration int? x;
. Maybe it could say something like:
The property 'x' is declared here.
Note that this isn't the only error message that could potentially be improved; it's just an example.