Using Null-Safety in new flutter projects only? - flutter

Currently I'm in Dart SDK version 2.10.4 (stable) and flutter 1.22.5 in my old projects. But now for my new projects, I want to use 2.12.0 version that has null-safety. My question is how to upgrade my dart sdk. And if I updated my sdk version, will it affects my old projects? Is it possible to only use dart 2.12.0 sdk version to my new projects?

First, check the dependency status where it checks whether dependencies inside the project are migrated to it.
Get the migration state of your package’s dependencies, using the following command:
dart pub outdated --mode=null-safety
it should give output
The latest column should be in green, if not then upgrade the dependencies of libs and check whether they support it to not.
Finally, Run the below command for migration
dart migrate

You can specify dart and flutter versions constraints in you pubspec.yaml for each project:
environment:
sdk: '>=2.12.0 <3.0.0'
flutter: '>=2.0.0'
So answer is yes, you can use new version of sdk with null safety in new projects and don't use it in your old projects. Just specify older version in pubspec for old projects.
In addition I advise you to migrate your old projects to null safety as soon as all of dependencies you used are migrated. Use this guide for it.

Related

How to upgrade flutter project dart sdk version

I have Flutter version 3.3.2 installed.
How do I upgrade flutter project dart sdk version?
Current project sdk version
sdk:">=2.7.0 3.0.0<"
You can run flutter upgrade and normally your project should pick up the most recent version of the SDK.
Refer to the documentation and to this previously asked question.

How to downgrade dart back to dart 1.x in Flutter?

I accidentally upgraded dart and flutter to the newest version but my code don't support null safety yet. Now I want to downgrade back to dart 1.x.
I tried flutter downgrade v1.2.1 but nothing changed. How can I downgrade back to dart 1.x?
You can downgrade flutter version by following below sequence.
type 'flutter downgrade 1.22.6'
Just type and command.
'flutter verion'
After 1,2 steps, you can see downgrading processing.
there is a tool that helps to manage different versions of flutter FVM
or you can use
flutter downgrade <version> (example: flutter downgrade v1.2.1)
If you want to downgrade flutter one the way which I use is to download the required SDK, unzip the folder and replace the existing flutter folder with the downloaded one.
Then restart the editor and you will the version you wanted.
Here are links for the Flutter SDK Release:
Windows
https://flutter.dev/docs/development/tools/sdk/releases
MacOS
https://flutter.dev/docs/development/tools/sdk/releases?tab=macos

How to update Dart SDK in ubuntu without updating Flutter?

So I am working on creating an e-commerce App in flutter. I am using flutter version 1.22.6 for Ubuntu (18.04.5 LTS). I don't want to upgrade my flutter version as it generated lots of issues to my colleagues. For me the problem is that i have to use a lot of packages for my Application which requires most recent Dart SDK. So is it possible to upgrade just the Dart SDK while keeping the same version of Flutter.
Probably not,
The reason for this is flutter is the framework and of course, it depends on dart language version. Why?
For example, If we compare 2 dart sdk between flutter 1.x version and flutter 2, we can see flutter ver 2 depends on dart sdk which supports null safety while flutter ver 1.x does not support null safety.
In the end, if you upgrade somehow the dart sdk without upgrading flutter sdk too, you will see many errors in flutter framework.

How to enable Null-Safety in Flutter?

I tried to use null safety, but it's giving me this error:
This requires the 'non-nullable' language feature to be enabled. Try
updating your pubspec.yaml to set the minimum SDK constraint to 2.10.0
or higher, and running 'pub get'.
I changed my Dart SDK constraint from 2.7.0 to 2.10.0, but it's still showing this error.
Also, I upgraded my Dart and Flutter SDK:
dart-sdk v2.10.2 is the latest version available based on your
source(s).
Flutter (Channel stable, 1.22.3, ...
Null safety is no longer an experiment as of Dart 2.12. It is now easy to enable.
Enabling null safety
Starting with the first Dart 2.12 versions, types will be non-nullable by default. So you just need to change your SDK constraint:
environment:
sdk: ">=2.12.0 <3.0.0"
Learn more about "Enabling null safety" on dart.dev.
To Enable null safety,
Check Latest Dart Version(It should be Dart 2.12 or later:)
dart --version
Update the dart version, the above point not satisfied using the command.
dart pub upgrade --null-safety
dart pub get
Run the below command to know what are libs in your project needs to be upgraded to the latest null safety.
dart pub outdated --mode=null-safety
The latest column shows the current version if it's in green that means dependency implemented null safety features if it red then the dependency owner needs to implement that.
Finally, run dart migration command which performs null safety migration on existing project(Existing project)
dart migrate
If your package is ready to migrate, then the tool produces a line like the following:
View the migration suggestions by visiting:
http://127.0.0.1:60278/Users/you/project/mypkg.console-simple?authToken=Xfz0jvpyeMI%3D
Note: Even after running upgrade --null-safety command, you see the latest column section in red, which means a particular dependency hasn't supported null safety yet, so that means you cannot migrate the project.
for detail read: https://dart.dev/null-safety/migration-guide
Good blog on Implementation of Null Safety: https://medium.com/flutterworld/flutter-null-safety-5d20012c2441
After Flutter 2.0.0 Stable u can run in app root folder:
dart migrate --apply-changes
This can happen when you upgrade the Flutter version you're using.
Try adding the following to the analysis_options.yml
analyzer:
- enable-experiment:
- non-nullable
Then, try cleaning and upgrading the project dependencies again.
To do that, you can use the following commands:
flutter clean
flutter packages pub upgrade
flutter pub run build_runner build
Finally, restart your IDE.
P.s. By the way, make sure that the sdk you're using is compatible with your Flutter version
Following the Dart documentation I was able to enable null safety in Flutter with these steps:
First add analysis_options.yaml:
analyzer:
enable-experiment:
- non-nullable
Then move to the dev channel and upgrade:
flutter channel dev
flutter upgrade
Change the sdk in pubspec.yaml
environment:
sdk: ">=2.11.0-213.0.dev <2.12.0"
Clean the project:
flutter clean
flutter pub get
Restart the IDE (VS Code in my case).
And then it was working fine.
Set the lower Dart SDK constraint to 2.12 in your pubspec.yaml file.
environment:
sdk: ">=2.12.0 <3.0.0"
Check if all the packages of your app are migrated to null safety.
dart pub outdated --mode=null-safety
If they are migrated, update all the packages to their null safe variant.
dart pub upgrade --null-safety
dart pub get
Start the migration process.
dart migrate
If you're happy with the changes, apply them using:
dart migrate --apply-changes
I had done all the above and yet for some reason the analyzer still gave an error about enabling null safety. What fixed it for me was running pub upgrade instead of pub get. I'm not even going to try to find out why, I'm just going to get back to work!
I confirm that evenIn 2022 it still works doing this...
Environment:
SDK: ">=2.12.0 <3.0.0"

How to run flutter projects with different/older Dart SDK dependencies?

The current Dart SDK version is 2.1.0-dev.9.4.flutter-f9ebf21297.
Because NewsBuzz requires SDK version >=1.8.0 <2.0.0, version solving failed.
I am trying to run projects from https://startflutter.com. There are several older projects using older versions of Dart SDK or with dependencies requiring older Dart SDK version.
Is there any way to run multiple versions of Dart without downloading the older version of Dart and changing the environment variables manually?
I have gone through several StackOverflow posts and lot of Github issues without finding a proper solution.
Flutter lower dart version
Where is Dart's SDK located within /flutter folder?
Specifying SDK version in the pubspec.yaml does not seem to help.
environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
Edit: Dart & Flutter support for Visual Studio Code docs says something about dart.sdkPaths and dart.flutterSdkPaths settings.
I had a similar problem:
My pubspec.yaml had the following:
environment:
sdk: ">=2.1.0 <3.0.0"
However when running flutter packages get I got the following error:
Running "flutter packages get" in xxx-app-mobile...
The current Dart SDK version is 2.1.0-dev.9.4.flutter-f9ebf21297.
Because xxx_app requires SDK version >=2.1.0 <3.0.0, version solving failed.
pub get failed (1)
I tried using the exact version name as the environment variable it did not work. I tried:
flutter upgrade
flutter clean
flutter update-packages
Received the same error when running flutter packages get
I then downgraded the version requirements in the pubspec.yaml to:
environment:
sdk: ">=2.0.0 <3.0.0"
And flutter packages get worked...
So I had an app that was developed on flutter 1.x SDK's, but after my completion, I moved on to build other projects and eventually shifted to fllutter2.0
As of now, flutter downgrade only lets you go one version back, Also going back from 2.x to 1.x seems difficult
So I went to this https://docs.flutter.dev/development/tools/sdk/releases site and downloaded the version i made the app in.
Then I had my dart-sdk for that specific project changed in Android Studio
Then I did the following
Deleted .idea folder and build folder as well as pubspec-lock file.
run flutter pub get
finally flutter build apk
Now this was a dire situation so I had to resort to this
I really will not recommend this unless you are in a rough spot
Simply update you flutter sdk to latest version.
First open your command (cmd) as an adminstrator then upgrade flutter : flutter upgrade it will do everything for you