I have the following code in a Flutter app that is currently running in unsound null safety mode (i.e. having both null-safe files and non-null-safe files).
int i = null;
String s = '';
double d = i ?? s;
This is obviously not correct, yet the Dart analyzer does not complain and the code compiles.
If I change the last line to
double d = null ?? s;
I get the expected error.
Shouldn't the Dart analyzer catch the error even when running in unsound null safety mode?
I have tried restarting the Dart analysis server, etc.
flutter doctor --verbose
[✓] Flutter (Channel stable, 2.10.2, on macOS 13.5.2 22G91 darwin-arm, locale en-SE)
• Flutter version 2.10.2 at /Users/username/fvm/versions/2.10.2
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 097d3313d8 (1 year, 8 months ago), 2022-02-18 19:33:08 -0600
• Engine revision a83ed0e5e3
• Dart version 2.16.1
• DevTools version 2.9.2
When you do:
the result of
i ?? s
is the nearest common base type ofi
ands
, which isObject
.Object
then is implicitly downcast todouble
, which, despite being potentially unsafe, was allowed by default in the days before null-safety.You can disallow implicit casts by setting
strict-casts: true
(orimplicit-casts: false
prior to Dart 2.16) in youranalysis_options.yaml
file, and then you will get a static analysis error: