Reputation: 2187
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
Upvotes: 49
Views: 87558
Reputation: 1
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.
Upvotes: 0
Reputation: 752
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.
Upvotes: 0
Reputation: 1
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
pubspec.yaml
it wasenvironment: sdk: ">=2.7.0 <3.0.0"
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.
Upvotes: 0
Reputation: 41
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.") }
Upvotes: 0
Reputation: 1736
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()
);
}
Upvotes: -1
Reputation: 349
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"
],
},
]
Upvotes: 15
Reputation: 17756
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
Upvotes: 67
Reputation: 1710
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
Upvotes: 8
Reputation: 1024
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:
pub global activate fvm
fvm install <version>
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.
Upvotes: 5
Reputation: 4819
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"
Upvotes: 39