100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Pub and Package Management

How Dart's pub tool, pubspec.yaml, and the pub.dev registry work together to manage dependencies and publish reusable packages.

Practical DartIntermediate9 min readJul 10, 2026
Analogies

What Is Pub?

pub is Dart's built-in package manager, and pub.dev is the official public registry where the Dart and Flutter community publishes reusable packages. Instead of writing every capability from scratch, a project declares the packages it needs in pubspec.yaml, and pub downloads and links them into the project.

🏏

Cricket analogy: Pub.dev acting as the central repository for Dart packages is like the ICC maintaining the official rankings and records that every cricket board across nations references — a single trusted source everyone pulls from.

The pubspec.yaml File

pubspec.yaml declares a project's name, description, version, and the environment SDK constraint it requires. Runtime packages go under dependencies, while tools needed only during development, such as test runners or code generators, go under dev_dependencies. Version constraints typically use caret syntax like ^1.2.0, which allows compatible minor and patch updates but blocks breaking major-version changes.

🏏

Cricket analogy: Specifying a caret version constraint like ^1.2.0 in pubspec.yaml is like a team management fixing a player's role for the season while allowing minor tactical tweaks match to match, but never changing the fundamental role.

Dependency Resolution and the Lockfile

Running dart pub get resolves every package's version constraints into a single compatible set and records the exact versions chosen in pubspec.lock, guaranteeing reproducible builds across machines. dart pub upgrade re-resolves to the newest versions still allowed by the constraints in pubspec.yaml, relying on the packages following semantic versioning to stay safe.

🏏

Cricket analogy: The pubspec.lock file freezing exact resolved versions is like a scorecard locking in the exact final XI that played a Test match — no ambiguity about who was actually on the field that day.

Publishing and Consuming Packages

A package's public API lives in its lib/ folder, and dart pub publish validates the pubspec, README, and LICENSE before uploading it to pub.dev for others to depend on. Before a package is officially published, developers commonly reference it with a path or git dependency in pubspec.yaml, pointing at a local folder or a repository instead of a published version.

🏏

Cricket analogy: Publishing a package to pub.dev with dart pub publish is like a cricket academy formally registering a promising young player with a state association — the API is now visible for the wider community to select and depend on.

yaml
name: weather_app
description: A sample app demonstrating pub dependency management.
version: 1.0.0
environment:
  sdk: '>=3.3.0 <4.0.0'

dependencies:
  http: ^1.2.0
  intl: ^0.19.0
  shared_preferences: ^2.2.2

dev_dependencies:
  test: ^1.25.0
  build_runner: ^2.4.9
  lints: ^3.0.0
bash
dart pub get
dart pub upgrade
dart pub outdated
dart pub publish --dry-run

Blindly running dart pub upgrade --major-versions can silently pull in breaking API changes, since caret constraints only protect against updates within the same major version. Always review a package's CHANGELOG.md before accepting a major version bump, and re-run your test suite immediately after upgrading.

  • pub is Dart's built-in package manager; pubspec.yaml declares a project's name, SDK constraint, dependencies, and dev_dependencies.
  • The caret syntax, e.g. ^1.2.0, allows automatic updates within the same major version but blocks breaking changes.
  • dart pub get resolves the dependency graph and writes exact resolved versions to pubspec.lock for reproducible builds.
  • dev_dependencies, like test and build_runner, are needed only during development and are excluded from published packages.
  • pub.dev is the official public registry; dart pub publish uploads a package after validating pubspec.yaml and lib/ structure.
  • path and git dependencies let you consume a package from a local folder or repository before it's officially published.
  • Semantic versioning (major.minor.patch) is the contract pub relies on to resolve compatible dependency versions automatically.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#DartStudyNotes#PubAndPackageManagement#Pub#Package#Management#Pubspec#StudyNotes#SkillVeris#ExamPrep