47

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

MRazaImtiaz
  • 1,964
  • 1
  • 13
  • 23
  • 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.x – AyadRocketfy Dec 15 '20 at 08:10
  • 1
    Thanks for your comment @AyadRocketfy but there is any other way so at least null safety doesn't affect my previous project. – MRazaImtiaz Dec 15 '20 at 08:15

8 Answers8

65

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
Miguel Ruivo
  • 16,035
  • 7
  • 57
  • 87
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"
jaredbaszler
  • 3,941
  • 2
  • 32
  • 40
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"
            ],
        },
]
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

Faiizii Awan
  • 1,615
  • 1
  • 13
  • 28
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.

Coda Veto
  • 896
  • 1
  • 8
  • 13
0

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()
  );
}
Manishyadav
  • 1,361
  • 1
  • 8
  • 26
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.") }

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.