Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn off Null Safety for previous Flutter Project?

I want to upgrade my flutter to get new features such as null safety, but I didn't want my previous project to affect them. I want new changes only for my new flutter project, I want to run my old project similar to the old way. Is there any way? Please guide me through it.

Thank You

like image 736
MRazaImtiaz Avatar asked Dec 15 '20 08:12

MRazaImtiaz


People also ask

IS null safety good in Flutter?

Adding null safety to Dart and Flutter became a good solution to developer productivity issues and reduced the number of bugs. With null safety, now Flutter developers can specify which variables can be null and all the previously unnoticed errors will show up during static analysis.

Which Flutter version has null safety?

Enabling null safety Sound null safety is available in Dart 2.12 and Flutter 2.


Video Answer


3 Answers

Setting SDK constraints in your old project's pubspec.yaml file should be enough.

For example, the following, does not have null safety enabled:

environment:
  sdk: ">=2.11.0 <3.0.0"

You can also specify at the top of your Dart file to disable null checks for that file.

// @dart=2.9
like image 127
Miguel Ruivo Avatar answered Oct 17 '22 17:10

Miguel Ruivo


The above answer wasn't working for me after I upgraded to Dart v2.12 on the beta channel. So I found these options:

You can add this to the top of any dart file to disable null-safety checks.

// @dart=2.9

or similar to the answer above, I had to include a version prior to 2.12 to disable null safety. You would edit this line in the pubspec.yaml file.

environment:
  sdk: ">=2.11.0 <3.0.0"
like image 45
jaredbaszler Avatar answered Oct 17 '22 18:10

jaredbaszler


You can run flutter project without null safety with --no-sound-null-safety option with flutter run.

Also you can add this as an argument in launch.json in VSCode

"configurations": [
        {
            "name": "Flutter",
            "request": "launch",
            "type": "dart",
            "flutterMode": "debug",
            "args": [
                "--no-sound-null-safety"
            ],
        },
]
like image 14
the_code_builder Avatar answered Oct 17 '22 18:10

the_code_builder