'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
Solution 1:[1]
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
Solution 2:[2]
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"
],
},
]
Solution 3:[3]
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 as
**Recommended: **Update your project to null safety. Read more about null-safety
Solution 4:[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:
- To activate globally run
pub global activate fvm - To install a specific version of Flutter run
fvm install <version> - 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.
Solution 5:[5]
I had the same problem.
You need to get the packages again after upgrading to Flutter 2.0
Do this by running:
flutter pub get
Or by saving the pubspec.yaml file in VS code, which will get the packages for you.
Solution 6:[6]
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()
);
}
Solution 7:[7]
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.") }
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 | the_code_builder |
| Solution 3 | Faiizii Awan |
| Solution 4 | Coda Veto |
| Solution 5 | Asjad Siddiqui |
| Solution 6 | Manishyadav |
| Solution 7 | Joseph Kibira |
