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"
Related
On a team it is important to make sure nobody upgrades a project accidentally, or does any builds of any kind on an unexpected version of flutter, via pipeline or locally or any deployment channel.
https://github.com/passsy/flutter_wrapper/issues/30
https://github.com/flutter/website/issues/1845
You can specify the Flutter version in the pubspec.yaml file under the "environment:" section. Reference here. Here is what my pubspec.yaml looks like (notice that the line for "sdk" is the Dart version):
environment:
sdk: ">=2.17.1 <3.0.0"
flutter: 3.0.2
For example, if my local Flutter version is 3.0.1 and I specify 3.0.2, pub get outputs the following for me:
Running "flutter pub get" in my_project...
The current Flutter SDK version is 3.0.1.
Because my_project requires Flutter SDK version >=3.0.2, version solving failed.
pub get failed (1; Because my_project requires Flutter SDK version >=3.0.2, version solving failed.)
Process finished with exit code 1
But notice how the warning message says that my_project requires "greater than or equal to" instead of being pegged at precisely 3.0.2. I believe this is what Jeppe's comment refers to as an open issue.
I try to migrate a flutter project to null-safety. I get the feedback that all packages are compatible and upgrated.
When I run dart migrate, my own writen screens (which I want to migrate) are criticized as unmigrated dependencies.
1)
$ dart pub outdated --mode=null-safety
Resolving...
Showing dependencies that are currently not opted in to null-safety.
[x] indicates versions without null safety support.
[+] indicates versions opting in to null safety.
Computing null safety support...
All your dependencies declare support for null-safety.
$ dart pub upgrade --null-safety
...
dio: ^4.0.5-beta1 -> ^4.0.4
firebase_core: ^1.11.0 -> ^1.12.0
$ dart pub get
...
Got dependencies!
$ dart migrate
Analyzing project...
[-------------------------------------------------/]
Bad state: Error: package has unmigrated dependencies.
Before migrating your package, we recommend ensuring that every library it
imports (either directly or indirectly) has been migrated to null safety, so
that you will be able to run your unit tests in sound null checking mode. You
are currently importing the following non-null-safe libraries:
package:prosz/Screens/bottombar/tabbar.dart
package:prosz/Screens/chat.dart
package:brosz/Screens/chatPost.dart
...
Please upgrade the packages containing these libraries to null safe versions
before continuing. To see what null safe package versions are available, run
the following command: `dart pub outdated --mode=null-safety`.
To skip this check and try to migrate anyway, re-run with the flag
`--skip-import-check`.
versions
$ flutter --version
Flutter 2.8.1 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 77d935af4d (7 weeks ago) • 2021-12-16 08:37:33 -0800
Engine • revision 890a5fca2e
Tools • Dart 2.15.1
pubspec
environment:
sdk: ">=2.1.0 <3.0.0"
changing pubspec to sdk: ">=2.12.0 <3.0.0" fixes the problem, but causes:
311 analysis issues found
.....
The migration tool didn't start, due to analysis errors.
The following steps might fix your problem:
1. Set the lower SDK constraint (in pubspec.yaml) to a version before 2.12.
2. Run `dart pub get`.
3. Try running `dart migrate` again.
Change 2.1.0 to 2.12.0 in your pubspec
here is my own answer: the number of analysis issues found are not the same when run "dart migrate" form ">=2.1.0 <3.0.0" and ">=2.12.0 <3.0.0".
There are much more when I run from ">=2.12.0 <3.0.0". (and especially wrong ones, because dart thinks the code has already been migrated)
So the right way is to start "dart migrate" from ">=2.1.0 <3.0.0" and fix the analysis issues found by hand. These analysis issues are mainly syntax changes of the upgraded packages, which can be found on pub.dev, in the respective changelogs
You get in this trouble if you start migration from flutter 2.8.1.
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.
New to Flutter so sorry about the question. Tried to change the environment but project won't build.
The current Flutter SDK version is 1.9.1+hotfix.6.
Because solemaster depends on google_sign_in >=4.0.16 which requires Flutter SDK version
>=1.10.0 <2.0.0, version solving failed.
pub get failed (1)
environment:
sdk: ">=2.1.0 <3.0.0"
The flutter SDK version you're using is older than the required by the library. You're confusing the Flutter SDK with the Dart SDK. The Dark SDK is the environment on the code you provided.
google_sign_in is based on the Flutter beta channel, instead of the stable channel. (p.s. this channel is where you get your flutter updates from).
From there, you have two options:
Use an older version of google_sign_in. Version 4.0.15 or 4.0.14 should do the trick, since this change was published today as said on the changelog.
On the pubspec.yaml file, change the current declaration to google_sign_in: 4.0.15.
Change to the beta channel. Keep in mind that the Flutter versions here are being tested, so you may experience some issues. To change channel, run flutter channel beta, then flutter upgrade.
try
flutter pub cache repair
flutter clean
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