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.
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
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
1. Which file declares a Dart project's dependencies and SDK version constraint?
2. What does a caret constraint like ^2.1.0 allow?
3. What is the purpose of pubspec.lock?
4. Where should packages only needed during development, like build_runner, be declared?
5. Which command uploads a package to pub.dev?
Was this page helpful?
You May Also Like
Dart and Flutter Overview
An introduction to the Dart language, how it powers Flutter's UI framework, and where else Dart is used beyond mobile app development.
Testing Dart Code
How to write reliable unit, mock-based, and widget or integration tests for Dart and Flutter code using package:test and Mockito.
Dart for Server-Side Development
Building and deploying backend services in Dart using dart:io and the shelf framework, from routing to native executable deployment.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics