117

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

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • The YAML spec you linked to, is a bit outdated, the latest version is 1.2. At the bottom you'll find an index, and if `^` were a special token for YAML (it isn't) you would find a link there. Independent of that for many the YAML spec doesn't make much sense ;-) – Anthon Nov 30 '18 at 18:51

3 Answers3

165

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

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

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • on what basis we added 4.0.0? similar 2.0.0. How we are identifying max versions? – Jitesh Mohite Jul 02 '19 at 10:30
  • 2
    @jitsm555, the max version is the next major number past the min version. If the min version is `3.1.5`, the major version number is `3`. The next number after `3` is `4` so the the max version is `4.0.0` (that is, anything less than `4.0.0` but not including `4.0.0` itself). – Suragch Aug 28 '19 at 01:17
  • @Suragch Is an update from `2.0.0` to `2.1.0` also considered a breaking change? – iDecode Apr 09 '21 at 14:05
  • @iDecode, No, `2.0.0` to `2.1.0` is not a breaking change. – Suragch Apr 10 '21 at 04:55
  • 1
    @Suragch Thanks, that means both `2.1.0` and `2.0.1` are not breaking changes for a package at `2.0.0`. – iDecode Apr 10 '21 at 12:08
  • Please help me understand. "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' " I think it includes the next non-breaking. The next 'breaking' is 4.0.0 which it will not include that, yes, anything before that is 'non-breaking' right? So it includes all versions including the next non-breaking, until 4.0.0(which is the breaking change). I may be thinking about it wrong tho' – Frankdroid7 Jun 21 '23 at 08:27
  • @Frankdroid7, correct. – Suragch Jun 21 '23 at 12:07
21

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

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
1

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

Boken
  • 4,825
  • 10
  • 32
  • 42
parth jansari
  • 138
  • 1
  • 6