Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart "--no-sound-null-safety" flag not working

I am trying to run a dart file without null safety using the command line.

The file is:

sandbox.dart

void main() {
    String a;
    print(a);
}

I then run the file using:

$ dart --no-sound-null-safety run sandbox.dart

This is described here: https://dart.dev/null-safety/unsound-null-safety

However, I still get the error:

Error: Non-nullable variable 'a' must be assigned before it can be used.
print(a);

I am using dart version;

Dart SDK version: 2.12.4 (stable) (Thu Apr 15 12:26:53 2021 +0200) on "windows_x64"

Why is it not working?

Note: It works by adding the version string at the top:

// @dart=2.9

But not the command line.

like image 892
Yahya Uddin Avatar asked Dec 12 '25 02:12

Yahya Uddin


1 Answers

You have to understand the Unsound null safety and how it works in different dart versions.

Dart provides null safety meaning values can't be null unless you allow them to be by using ? so it becomes:

void main() {
    String? a;
    print(a);
}

This returns a null instead of the nun-nullable error.

Using language version 2.9 for a library that’s in a 2.12 package can reduce analysis errors (red squiggles) coming from unmigrated code. However, unsound null safety reduces the information the analyzer can use. For example, the analyzer might assume a parameter type is non-nullable, even though a 2.9 file might pass in a null value.

like image 104
Clinton Njiru Avatar answered Dec 15 '25 12:12

Clinton Njiru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!