Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Package Managers  ›  Capstone Project

Version Constraints and the Lock File

flutter pub / dart pub 15 minA caret constraint in pubspec.yaml allows compatible versions up to the next breaking release, while pubspec.lock records the exact versions chosen so every build matches.

Version Constraints and the Lock File

The http line in Sprout's pubspec.yaml says ^1.2.0, yet your machine is using version 1.2.2. A teammate's might pick something else next month. To reason about that, you need to read two things: the constraint you wrote, and the lock file pub wrote for you.

The rule

The caret allows everything up to the next breaking version; the lock file pins one.

Dart packages use semantic versioning: major.minor.patch.

  • Patch (1.2.2): bug fixes.
  • Minor (1.3.0): new features, nothing removed.
  • Major (2.0.0): may break existing code.

A caret constraint accepts any version that should be compatible:

Constraint Allows Stops before
^1.2.0 1.2.0, 1.2.9, 1.9.4 2.0.0
^0.4.1 0.4.1, 0.4.7 0.5.0

For packages still below 1.0, the minor number plays the role of the breaking one, so the caret stops at the next minor.

Other forms exist, such as an exact 1.2.0 or a range '>=1.2.0 <1.5.0', but the caret is the normal choice and what pub add writes.

pubspec.lock records the single version pub actually chose for every package, including the packages your packages depend on:

  http:
    dependency: "direct main"
    source: hosted
    version: "1.2.2"

flutter pub get reuses those locked versions. Only flutter pub upgrade (or changing a constraint) chooses new ones.

The shortcut

To read a caret, bump the major number; if the major is 0, bump the minor instead. That is the ceiling.

Reading Sprout's constraintsStep 1 of 4
  1. ^1.2.0

    The major is 1, so bump it: ceiling 2.0.0. Allowed: 1.2.0 up to, not including, 2.0.0.

The trap is expecting flutter pub upgrade to jump to a new major version. It stays inside your constraints; crossing a major is a deliberate edit.

Drills

Answer fast — trust the rule.

quick drill

Which version does ^1.2.0 allow?

quick drill

What is the ceiling of ^0.4.1?

quick drill

What does pubspec.lock record?

quick drill

http: ^1.2.0 is locked at 1.2.2, and 2.0.0 has just been released. What does flutter pub upgrade do?

quick drill

In semantic versioning, which change signals that existing code may break?

Remember

  • The caret allows everything up to the next breaking version; the lock file pins one.
  • Bump the major to get the ceiling; if the major is 0, bump the minor.
  • Trap: upgrade never crosses a major version by itself.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainRead the caret

Sprout's pubspec.yaml has http: ^1.2.0 and a small package at ^0.4.1. Explain which versions each constraint allows and why, and describe what pubspec.lock adds on top of these constraints.