Turn off Null Safety for previous Flutter Project?
Asked Answered
T

10

48

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

Tetradymite answered 15/12, 2020 at 8:4 Comment(2)
I recommend you to upgrade Flutter with the command line using Flutter upgrade and then working on the new project with this version. When you want to work on previous project just downgrade to your current version using Flutter downgrade v1.x.xSubservient
Thanks for your comment @Subservient but there is any other way so at least null safety doesn't affect my previous project.Tetradymite
R
67

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
Rollie answered 16/12, 2020 at 12:4 Comment(5)
Can I still use flutter 2 widgets with this. This should not be a problem right?Pareira
@MiguelRuivo despite doing so, I can't disable null-safety I get this error: The current Dart SDK version is 2.12.2. Because onapp requires SDK version 2.7.0, version solving failedMalaise
Make sure you run flutter pub get after updating the constraints.Rollie
If we need it to be less than 2.12.0 shouldn't this read: >=2.11.0 <2.12.0?Auxiliaries
In my case sdk: ">=2.7.0 <3.0.0" was enough to make a reused project to get rid of Null safety errors.Calie
C
39

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"
Compote answered 2/2, 2021 at 19:36 Comment(3)
What if you're already started with optionals etc?Lichen
The solution is to downgrade? So what's the point in the development team making the new versions of dart and flutter then?Wingspan
@Wingspan - Just because this answer is here, doesn't mean people need to use it. Consider this question/answer was around the time that null safety was becoming a thing. And if people didn't want to take on the challenge of converting their project to null safety at the time, they needed a way around it. And based on the number of upvotes on this answer and the one above, I'd say that's helpful to a lot of people.Compote
V
15

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"
            ],
        },
]
Voelker answered 18/6, 2021 at 8:28 Comment(3)
Where is launch.json file?Puttergill
@Puttergill you can create at root of your project ./vscode/launch.jsonVoelker
I found it in VS Code Menu Open Run Menu -> Open Configuration.Puttergill
Q
8

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

Quatre answered 26/4, 2021 at 8:31 Comment(0)
B
4

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.

Busby answered 4/4, 2021 at 16:14 Comment(0)
O
0

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.") }

Ober answered 13/5, 2022 at 6:6 Comment(0)
C
0

I had to downgrade my flutter SDK according to the dart version. I was using latest Flutter SDK 3.10.5 with Dart version 3.0.5 and were having same issue. So I followed this

  1. Checked environment in pubspec.yaml it was

environment: sdk: ">=2.7.0 <3.0.0"

  1. Downloaded the flutter SDK from the SDK archive with the Dart version that was not falling in the above mentioned range. In my case it was Flutter SDK 3.7.12 BECAUSE it had the Dart version 2.19.6 which was not falling in the >=2.7.0 <3.0.0 range.

Rebuild the app & it WORKED like a charm.

Carrasquillo answered 17/6, 2023 at 17:30 Comment(1)
Don't forget to run flutter clean after that.Cheery
C
0

Since a right answer has been chosen, I'll post this for those people who're planning to phase in the upgrade. Here's a bash script that will help you put the // @dart=2.9 comment at the very top of all your .dart files within your lib directory:

#!/bin/bash

process_files() {
  for file in "$1"/*; do
    if [ -d "$file" ]; then
      process_files "$file"
    elif [ "${file##*.}" = "dart" ]; then
      sed -i '1s/^/\/\/ @dart=2.9\n/' "$file"
      echo "Comment added to $file"
    fi
  done
}

main_directory="./lib"

process_files "$main_directory"

I hope it helps save you some time.

Crescin answered 6/9, 2023 at 23:22 Comment(0)
H
0

Follow the following steps in VS Code to turn off the Null Safety Feature in Flutter:

1.Go to settings.

2.In the search bar type "Flutter Run Additional Args".

3.Remove the null safety item.

Herwig answered 28/1 at 9:30 Comment(0)
G
-1

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()
  );
}
Germanium answered 23/3, 2022 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.