TExcelTAD
Home › Dart › Packages & pub.dev

Packages & pub.dev

A package is a directory with a pubspec.yaml file and some Dart code — usually under lib/. Your own app is a package, and so are the tens of thousands of reusable libraries on pub.dev, Dart's official package repository. The dart pub tool downloads them, resolves versions and keeps everything consistent.

Note: Package commands need a real machine with the SDK and network access, so the terminal examples on this page are simulated.

pubspec.yaml

yaml
name: weather_cli
description: Shows the weather in your terminal.
version: 1.2.0
publish_to: none          # remove to allow `dart pub publish`

environment:
  sdk: ^3.7.0

dependencies:             # needed at runtime
  http: ^1.2.2
  args: ^2.6.0

dev_dependencies:         # only for development: tests, lints, generators
  test: ^1.25.0
  lints: ^5.1.0
Field Meaning
name Package name — used in package:weather_cli/... imports
environment.sdk Which Dart versions are supported
dependencies Packages your code imports
dev_dependencies Packages used only by tests and tooling
version Your package's own semantic version

Adding dependencies

dart pub add edits pubspec.yaml and downloads the package in one step:

bash · try it yourself
dart pub add http
Resolving dependencies...
Downloading packages...
+ http 1.2.2
+ http_parser 4.1.1
+ typed_data 1.4.0
  ...
Changed 6 dependencies!
bash · try it yourself
dart pub add --dev test
Resolving dependencies...
Downloading packages...
+ test 1.25.15
+ test_api 0.7.4
+ test_core 0.6.8
  ...
Changed 41 dependencies!

Then import it with the package: scheme:

dart
import 'package:http/http.dart' as http;

Future<void> main() async {
  final response = await http.get(Uri.parse('https://example.com/api/weather'));
  print(response.statusCode);
}

Version constraints

Dart packages follow semantic versioning: MAJOR.MINOR.PATCH. Breaking changes bump MAJOR.

Constraint Allows
^1.2.2 (caret, recommended) >=1.2.2 <2.0.0
^0.4.1 >=0.4.1 <0.5.0 (for 0.x, the minor is "major")
'>=1.0.0 <3.0.0' An explicit range
1.2.2 Exactly that version (avoid — causes conflicts)
any Anything (avoid)

pubspec.lock

When pub resolves your dependencies, it writes the exact versions it chose to pubspec.lock.

  • Apps: commit pubspec.lock, so every developer and CI build uses identical versions.
  • Packages you publish: don't commit it; consumers resolve their own versions.

Everyday commands

Command What it does
dart pub get Download everything in pubspec.yaml (respecting the lock file)
dart pub upgrade Move to the newest versions the constraints allow
dart pub upgrade --major-versions Also raise constraints to new major versions
dart pub outdated Show which dependencies have newer versions
dart pub remove <pkg> Remove a dependency
dart pub deps Print the dependency tree
dart pub publish Upload your package to pub.dev
bash · try it yourself
dart pub outdated
Showing outdated packages.
[*] indicates versions that are not the latest available.

Package Name  Current  Upgradable  Resolvable  Latest
direct dependencies:
args          *2.5.0   2.6.0       2.6.0       2.6.0
http          *1.1.2   1.2.2       1.2.2       1.2.2

2 upgradable dependencies are locked (in pubspec.lock) to older versions.
To update these dependencies, use `dart pub upgrade`.

Other dependency sources

yaml
dependencies:
  shared_models:
    path: ../shared_models          # a local folder (monorepos)
  experimental_lib:
    git:
      url: https://github.com/acme/experimental_lib.git
      ref: v0.3.0

Choosing a good package

On pub.dev, each package page shows signals that help you judge quality:

  • Likes and downloads — popularity.
  • Pub points (out of 160) — documentation, platform support, static analysis, up-to-date dependencies.
  • Verified publisher — e.g. dart.dev, google.dev, or a company domain.
  • Dart 3 compatible and recent releases.

Popular packages worth knowing: http / dio (networking), args (CLI parsing), path, intl (dates and translations), collection (extra collection helpers), freezed and json_serializable (code generation), test and mocktail (testing), shelf (servers).

Tip: Every dependency is code you ship and maintain. Before adding one for a ten-line helper, consider writing the ten lines.

Test yourself

Which section of pubspec.yaml should list test?

  1. dependencies
  2. dev_dependencies
  3. environment
  4. executables

Which versions does the constraint ^2.3.0 allow?

  1. Exactly 2.3.0
  2. >=2.3.0 <3.0.0
  3. >=2.3.0 <2.4.0
  4. Any version

Should an application commit its pubspec.lock file?

  1. Yes, so everyone builds with identical versions
  2. No, never
  3. Only on Windows
  4. Only for dev_dependencies