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
Small issue with the analyser regarding to nullsafety
I'm using version Dart SDK version: 2.14.2 (stable) (Wed Sep 15 12:32:06 2021 +0200) on "macos_x64"
We have the following flutter code. _controller is a nullable InAppWebViewController?. To not have to include a ! when we use it, we add a final final controller = _controller and do a check await controller?.canGoBack() == true. In this case, controller is never null, since controller?.anything will return null and null != true 😁. Therefore await controller.goBack(); should be fine, but it throws an error. The method 'goBack' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!').dart(unchecked_use_of_nullable_value)
no errors are thrown. And everything compiles.
If I simplify the code further, this becomes more apparent:
onBackClicked: () async {
final controller = _controller;
if (controller?.canGoBack == true) {
controller.goBack(); <-- The method 'goBack' can't be unconditionally invoked because the receiver can be 'null'.
return;
}
Is there any way that controller can be null?
Otherwise, I would suggest to check if the analyser can detect this and allow us to not have to check for null again.
The text was updated successfully, but these errors were encountered:
Dart promotes the types of variables when you do certain checks on the variable itself.
Here the controller != null check will promote the controller variable to a non-null type along the true branch.
The check controller?.canGoBack == true does not check the variable controller directly, so there is no promotion.
It's logically correct that if controller?.canGoBack is the non-null value true, then controller cannot be null, but that's more logical inference steps than the compiler will do for you.
We are aware of this short-coming (see dart-lang/language#1224). We haven't decided whether to increase the complexity of the non-null inference to support this case yet.
Small issue with the analyser regarding to nullsafety
I'm using version Dart SDK version: 2.14.2 (stable) (Wed Sep 15 12:32:06 2021 +0200) on "macos_x64"
We have the following flutter code.
_controller
is a nullableInAppWebViewController?
. To not have to include a!
when we use it, we add a finalfinal controller = _controller
and do a checkawait controller?.canGoBack() == true
. In this case, controller is never null, sincecontroller?.anything
will return null andnull != true
😁. Thereforeawait controller.goBack();
should be fine, but it throws an error.The method 'goBack' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!').dart(unchecked_use_of_nullable_value)
if we instead do
no errors are thrown. And everything compiles.
If I simplify the code further, this becomes more apparent:
Is there any way that controller can be null?
Otherwise, I would suggest to check if the analyser can detect this and allow us to not have to check for null again.
The text was updated successfully, but these errors were encountered: