Turn off Null Safety for previous Flutter Project?

Viewed 57041

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

7 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

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"

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"
            ],
        },
]

your project might be developed using flutter older(version below 2.+). The major change in 2.x.x dart version is enabling null safety. At this moment, a lot of libs on pub.dev have been upgraded to the null safety feature.

But your old project might have some libs which are still not updated to null safety. So, your project might have mixture of both. In this case @miguel answer is partially valid (Defining sdk: ">=2.7.0 <3.0.0" constraint). To run your project you also need to unsound the null-safety. like by running following command

flutter run --no-sound-null-safety

or add this command in configuration by go to Run->Edit Configurations. it will open the following popup and the highlighted string same asenter image description here

**Recommended: **Update your project to null safety. Read more about null-safety

I had the same problem and after doing a lot of downgrading and upgrading (especially when the old project needed to be built with the old version of Flutter and build_runner) I found out about Flutter Version Manager check the git repo here: https://github.com/leoafarias/fvm. The best thing about this is you can specify which version you want to use per project.

Following comes from the instructions in the repo:

  1. To activate globally run pub global activate fvm
  2. To install a specific version of Flutter run fvm install <version>
  3. Then go into the root of the project and run fvm use <version>

Et voila! Hope it helps .

Check the repo for more commands like fvm use <version> --global to easily switch global versions and more interesting stuff.

You can declare the variables using var or dynamic and now the compiler won't check the variable type.

var answerText;
dynamic answerColor;

Answer({this.answerText, this.answerColor});

And if your try to build without the null safety checking then use this comment line // @dart=2.9 in the main.dart first line.

// @dart=2.9

import 'package:flutter/material.dart';

void main() async {
  
  runApp(
    const MyApp()
  );
}

Delete the following line of code from gradle file or just comment it out to run the code without the Gradle Exeception- if (flutterRoot == null) { throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") }

Related