'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.



Solution 1:[1]

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.

Solution 2:[2]

You have two way to handle it.

  1. You can Ignore it by pasting --no-sound-null-safety command in Additional Run args( Android Studio Click on Drop Down on Top Middle with main.dart label and click on Edit Configuration paste --no-sound-null-safety command in additional run args).

  2. (Recommended) You can accept it and enjoy the latest feature by upgrading sdk version in PubSpec.yml minimum Sdk for null safety must be sdk: ">=2.12.0 <3.0.0". Hope this will work for you

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Kapil