What is the caret sign (^) before the dependency version number in Flutter's pubspec.yaml? - flutter

In the pubspec.yaml file of my Flutter project there is a caret (^) before the version number of some of the dependencies.
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
english_words: ^3.1.5
What is its purpose? What does it mean?
Notes
I looked in the yaml documentation but I didn't see anything that made sense.
Related: what does Caret sign do in Dart (but it isn't an XOR operator here)
Related: In Flutter, Dependencies must specify version number? (an answer pointed me in the right direction but the question is asking something different)

The caret sign (^) is used for pub dependencies in Dart to indicate a range of version numbers are allowed. Specifically, any version from the specified version up to (but not including) the next non-breaking version is ok.
So ^3.1.5 is the same as '>=3.1.5 <4.0.0'
And ^1.2.3 would be the same as '>=1.2.3 <2.0.0'
It's shorthand for the longer form.
The ^ is saying, I want to automatically use the most up-to-date package from Pub as long as that update won't break anything in my app.
Notes
The concept of Semantic Versioning is important here. Read the article at the link if you are not familiar with it.
Version constraints documentation
Caret syntax documentation
Clarification for versions less than 1.0.0
Originally I had thought that
^0.1.2 is the same as '>=0.1.2 <1.0.0' (wrong!)
However, that is an incorrect understanding of Semantic Versioning. When the major version number is 0 (as in the 0 of 0.1.2), the meaning is that the API is unstable and even minor version number changes (as in the 1 of 0.1.2) can indicate a breaking change.
The Semantic Versioning article states:
Major version zero (0.y.z) is for initial development. Anything may change at any time. The public API should not be considered stable.
and also
How should I deal with revisions in the 0.y.z initial development
phase?
The simplest thing to do is start your initial development release at
0.1.0 and then increment the minor version for each subsequent release.
Thus, the following is the corrected form:
^0.1.2 is the same as '>=0.1.2 <0.2.0'
Thank you to Günter Zöchbauer for pointing out my error.
See also
How do Dart Package Versions work & how should I version my Flutter Plugins?

The caret sign ^ means the specified version and all newer versions that don't introduce breaking changes relative to the specified version.
Dart follows Semantic Versioning and suggests that to be used for package maintainers as well.
Semantic Versioning defines that
For versions >= 1.0.0, the major version needs to be incremented for breaking changes.
For versions < 1.0.0, the minor version needs to be incremented for breaking changes.
Example:
^2.4.3 means >= 2.4.3 < 3.0.0
^0.17.19 means >= 0.17.19 <0.18.0

It means that any new versions of a dependency that does not contain breaking changes will be accepted.
Dart language follows semantic versioning and uses it for package maintainers.
In layman's terms ^1.8.1 means >= 1.8.1 < 2.0.0

Related

Version solving for certain package versions failed in flutter

I have received this kind error a couple of time before with different packages, usually i just try a combination of different versions till i finally get a match that works. Is there a way to know what package versions are compatible instead of using the trial error approach which is outright time consuming and exhausting.
Initially i though it could be that one of the packages depends on the other, like in this case maybe flutter_svg depends on a different version of flutter_luban so the version in my pubspec clashes with it, but none of these packages depend on one another after viewing the respective package dependencies.
pub get failed (1; So, because sakaHapa depends on both
flutter_svg ^0.17.4 and flutter_luban ^0.1.13, version solving
failed.)
You can use 'pub outdated' command like below.
https://dart.dev/tools/pub/cmd/pub-outdated
Here is column what means.
Current
The version used in your package, as recorded in pubspec.lock. If the package isn’t in pubspec.lock, the value is -.
Upgradable
The latest version allowed by your pubspec.yaml file. This is the version that dart pub upgrade resolves to. The value is - if the value in the Current column is -.
Resolvable
The latest version that can be resolved, when combined with all other dependencies. This version corresponds to what dart pub upgrade gives you if all version constraints in pubspec.yaml are unbounded. A value of - means that the package won’t be needed.
Latest
The latest version of the package available, excluding prereleases unless you use the option --prereleases.
flutter pub outdated

My project uses '>=2.14.4 <3.0.0' of dart,but doing '>=2.11.9 <3.0.0' removes null safety.So does it select '2.11.9' from this range?

If yes,then from where does my VS code get it if I have dart sdk 2.14.4 installed on my device
Those constraints are the minimum and maximum versions.
If you give a minimum version of 2.11.9, you cannot use null safety (came with 2.12.0), because it did not exist in 2.11.9. You promised your project would work with all versions from 2.11.9 to anything smaller than 3. And your compiler enforces that promise.
It doesn't matter what version you have installed.

Dart: What is the difference between the "Upgradable" and "Resolvable" columns in the output of "dart pub outdated"?

What is the difference between the "Upgradable" and "Resolvable" columns in the output of "dart pub outdated"?
Here is an example of a package that can be upgraded to the latest. The Upgradable, Resolvable, and Latest all match:
url_launcher *6.0.11 6.0.12 6.0.12 6.0.12
Here is an example of a package that is already at the highest resolvable version, but can't be upgraded to the absolute latest version. Presumably another dependency is restricting the resolvability to the latest.
rxdart *0.26.0 *0.26.0 *0.26.0 0.27.2
Here is an example of a package that can't be upgraded any higher but has a Resolvable version that is higher. What does this mean? How is this different from the middle case above?
provider *5.0.0 *5.0.0 6.0.1 6.0.1
In addition to the other very useful posted thoughts, I have learned the following helpful details:
Upgradable refers to the highest version that your direct pubspec will permit, which considers the sdk version and the individual package version (whether to upgrade to just minor or also major). When Upgradable is limited, it can likely be your sdk version holding things back. In my case I was using sdk 2.12, but some packages require 2.14 now (Sep 2021).
From the docs:
The latest version allowed by your pubspec.yaml file. This is the version that dart pub upgrade resolves to. The value is - if the value in the Current column is -.
Resolvable refers to the highest version that all of the other packages' dependencies will allow, in addition to your direct pubspec constraints. When the Resolvable is limited, there is usually one package holding everything back, or a major version holding everything back.
From the docs:
The latest version that can be resolved, when combined with all other dependencies. This version corresponds to what dart pub upgrade gives you if all version constraints in pubspec.yaml are unbounded. A value of - means that the package won’t be needed.
Upgradable means an upgradeable version. Generally, minor version updates can be upgraded directly without modification.
Resolvable can use the version, generally a major version update (the 0.x version may have destructive changes, same as major version), may have incompatible interfaces to the previous version, if you update this version, you may need to change some code
Latest The latest version. If it is inconsistent with the Resolvable version, means the SDK version required by the latest version is inconsistent with the current project
The main difference is that Resolvable mains the version you need to reach to resolve outdated issues in your project and Upgradable mains the version you can update of that package.
To resolve it you must find packages you can update and continue updating until you can update the main package.
You can see it in this link about those concepts: Dart pub outdated

Why is Dart 2.12 a minor SDK upgrade when it breaks the previous code?

Currently, I'm have the following Dart SDK in my pubspec.yaml file.
environment:
sdk: ">=2.6.0 <3.0.0"
But if I change it to the following and run flutter pub get
environment:
sdk: ">=2.12.0 <3.0.0"
I start getting errors for nullability. As far as I know this is supposed to be a minor change according to language versioning but I broke my code. Should't the addition of Null safety in Dart SDK considered a major upgrade like 3.0.0 as it breaks the existing code.
Note: I didn't run any command to opt-in for null safety.
Let me try wording Randal Schwartz's answer in a different way:
Using the Dart 2.12 SDK does not require using null safety. You can replace your existing Dart SDK installation with the Dart 2.12 SDK to use 2.12's tooling (e.g. to take advantage of better optimizations in the compiler, use new lints supported by the analyzer, new behavior in the formatter) and library implementations (i.e., to get bug fixes). If that's only thing that you do, your existing code should not be broken.
Although enabling null safety breaks old code, the null safety feature is opt-in. The mechanism to opt in to use null safety is to separately specify a minimum version of Dart 2.12 in your SDK requirements. When you specify:
environment:
sdk: ">=2.6.0 <3.0.0"
that means that your code must remain compatible with Dart SDK version 2.6.0 (and therefore should not use any language features introduced after 2.6.0). Since the Dart 2.12 SDK can provide such backward compatibility, it did not technically require a major version bump.
Dart 2.12 didn't break the code. You did. If you change the minsdk to 2.12.0, it won't work until you fix everything, because it's now running (as you asked it to do) in NNBD mode. Dart 2.12 was happy to run it for you in non-null-safe mode, but you changed it. So, no, this is not a breaking change. Legacy code still runs fine with Dart running in legacy mode.

How to upgrade dev dependencies in Flutter

How can I upgrade my Flutter dev_dependencies? The flutter pub upgrade command doesn't seem to work on those and I don't see a --dev parameter for the command so do I have to update them manually or am I doing something wrong?
Are you familiar with the carat syntax? (e.g., dependency: ^x.y.z)
Carat Syntax documentation
Essentially, the carat indicates to automatically get the most up to date version of that package that is backward compatible with the number you specify. So it will update automatically up to a major version, at which point you will need to manually specify the new version number.