Question #94MediumFlutter Basics

pubspec.yaml - package versions : - ^0.13.0

Answer

Overview

The

text
^
(caret) symbol in
text
pubspec.yaml
enables compatible version updates using semantic versioning.
text
^0.13.0
means "any version from 0.13.0 up to (but not including) 0.14.0".


Semantic Versioning (SemVer)

Flutter packages follow SemVer format:

text
MAJOR.MINOR.PATCH

text
^1.2.3
 │ │ │
 │ │ └─ PATCH: Bug fixes (backward compatible)
 │ └─── MINOR: New features (backward compatible)
 └───── MAJOR: Breaking changes

Caret (^) Version Constraint

Syntax:

text
^version
allows updates that do not change the left-most non-zero digit.

yaml
dependencies:
  http: ^0.13.0  # 0.13.0 <= version < 0.14.0
  provider: ^6.0.5  # 6.0.5 <= version < 7.0.0

Examples

ConstraintAllowed VersionsNot Allowed
text
^0.13.0
0.13.0, 0.13.5, 0.13.990.14.0, 1.0.0
text
^1.2.3
1.2.3, 1.5.0, 1.99.02.0.0
text
^2.0.0
2.0.0, 2.1.0, 2.99.03.0.0
text
^0.0.5
0.0.5 only0.0.6, 0.1.0

Why Use ^?

Automatic compatible updates without breaking your app.

yaml
# ✅ With caret — gets bug fixes automatically
dependencies:
  flutter_bloc: ^8.1.0  # Receives 8.1.1, 8.2.0, etc. (no breaking changes)

# ❌ Without caret — stuck on exact version
dependencies:
  flutter_bloc: 8.1.0  # Never updates (even for critical bug fixes)

Other Version Constraints

Exact Version

yaml
dependencies:
  http: 0.13.0  # Only 0.13.0 (no updates)

Range

yaml
dependencies:
  http: '>=0.13.0 <0.15.0'  # 0.13.0 to 0.14.x (not 0.15.0)

Any Version

yaml
dependencies:
  http: any  # Latest version (not recommended)

Greater Than or Equal

yaml
dependencies:
  http: '>=0.13.0'  # 0.13.0 or newer (no upper limit)

Special Case: 0.x.y Versions

Pre-1.0 packages are considered unstable. Minor updates may include breaking changes.

yaml
# ^0.13.0 is strict for pre-1.0 versions
dependencies:
  package_a: ^0.13.0  # 0.13.0 <= v < 0.14.0 (minor = potential breaking)
  package_b: ^1.2.0   # 1.2.0 <= v < 2.0.0 (major = breaking)

How Flutter Resolves Versions

When you run

text
flutter pub get
, Flutter finds a version that satisfies all constraints.

yaml
# Your app
dependencies:
  http: ^0.13.0      # Allows 0.13.x

# Another dependency needs http
dependencies:
  http: ^0.13.5      # Also allows 0.13.x

# Flutter installs: 0.13.6 (latest that satisfies both)

Common Patterns

yaml
dependencies:
  # Stable packages (1.0+) — use caret
  provider: ^6.0.0
  http: ^1.1.0

  # Pre-1.0 packages — caret for minor updates
  some_new_package: ^0.5.0  # Gets 0.5.x (not 0.6.0)

  # Flutter SDK constraints
  sdk: ">=3.0.0 <4.0.0"

  # Git dependencies (exact commit/tag)
  custom_package:
    git:
      url: https://github.com/user/repo.git
      ref: v1.2.3  # Specific tag

Updating Dependencies

bash
# Check for available updates
flutter pub outdated

# Update within constraints (respects ^)
flutter pub upgrade

# Update to latest (ignores constraints)
flutter pub upgrade --major-versions

Best Practices

yaml
# ✅ Use caret for stable packages
dependencies:
  http: ^1.0.0

# ✅ Lock critical dependencies
dependencies:
  payment_sdk: 2.5.3  # Exact version (no surprises)

# ❌ Avoid 'any' (unpredictable)
dependencies:
  random_package: any

# ✅ Set SDK constraints
environment:
  sdk: ">=3.0.0 <4.0.0"
  flutter: ">=3.10.0"

Version Conflict Example

yaml
# App dependency
dependencies:
  package_a: ^1.0.0  # Depends on shared_lib: ^2.0.0
  package_b: ^2.0.0  # Depends on shared_lib: ^3.0.0

# ❌ Conflict! Cannot satisfy both
# Solution: Update package_a or package_b

pubspec.lock

After

text
flutter pub get
, exact versions are saved in
text
pubspec.lock
:

yaml
# pubspec.lock (auto-generated)
packages:
  http:
    version: "0.13.6"  # Exact installed version

Commit

text
pubspec.lock
to ensure team uses same versions.


Summary Table

SymbolMeaningExampleAllowed
text
^
Compatible updates
text
^1.2.3
1.2.3 - 1.x.x
NoneExact version
text
1.2.3
1.2.3 only
text
>=
Greater/equal
text
>=1.2.3
1.2.3+
RangeExplicit range
text
>=1.0.0 <2.0.0
1.x.x
text
any
Latest
text
any
Any version

Learn more: Dart Package Versioning