DartIntermediate 2 min read

Dart pubspec and dependencies: constraints, lockfiles and upgrades

Manage a reproducible application dependency graph without hiding incompatible SDK requirements.

On this page

The pubspec describes your package; a lockfile records a particular dependency resolution. SDK constraints also determine the language version available to your source, so an installed new SDK does not automatically enable every new syntax in an old package.

Start with explicit metadata

name: invoice_tools
publish_to: none
environment:
  sdk: ^3.14.0
dependencies:
  http: ^1.6.0
dev_dependencies:
  test: ^1.33.0

This is a local learning application’s example. Add runtime dependencies for code used by the application and dev dependencies for tooling such as tests. Do not copy a version combination without checking the package’s current SDK and platform support.

Run dart pub get to resolve the graph, then dart pub outdated to inspect available changes. A major version increase can change an API even when the package name stays the same. Review one upgrade at a time and rerun the affected tests.

Keep application builds reproducible

Commit an application’s lockfile so another checkout can reproduce its selected graph. A reusable package has different consumer-resolution concerns; follow the package publication conventions rather than assuming a library’s lockfile controls its users.

Use dependency overrides only for an intentional, documented local investigation. They do not prove that an incompatible transitive dependency is safe. A failed solver result usually contains a chain of constraints that identifies the actual conflict.

Understand generated state

.dart_tool/ is tooling output, not application source. Removing it should not be the first reaction to a clear SDK-constraint error. A package import uses the package graph, not the relative location of a copied pub cache file.

Exercise

Add a small test dependency with dart pub add --dev test, inspect the pubspec/lockfile diff and run the tests. Explain the difference between updating the SDK constraint, changing a dependency constraint and changing the resolved lockfile. Continue with library boundaries before publishing anything.

How this topic connects

Apply it next

Lesson progressMark complete after checking the example and exercise.

Find your next idea

Search lessons, code examples and projects.

Type a few words to start exploring.