Packages, pub and pubspec.yaml

30.1 pubspec.yaml

name: weather_cli
description: A command-line weather tool.
version: 1.2.0
repository: https://github.com/me/weather_cli
publish_to: none            # remove to allow publishing to pub.dev

environment:
  sdk: ^3.5.0

dependencies:
  http: ^1.2.0              # caret: >=1.2.0 <2.0.0
  args: ^2.5.0
  path: any
  my_local_pkg:
    path: ../my_local_pkg
  my_git_pkg:
    git:
      url: https://github.com/me/pkg.git
      ref: main

dev_dependencies:
  test: ^1.25.0
  lints: ^5.0.0
  build_runner: ^2.4.0

executables:
  weather: main              # `dart pub global activate` exposes the `weather` command

dependency_overrides:
  http: 1.2.1                # force a version (use temporarily)

30.2 pub Commands

Workspaces (Dart 3.6) support monorepos with a shared resolution (workspace: in the root pubspec, resolution: workspace in members). Melos is the popular monorepo tool.

pub.dev scores packages (pub points, popularity, likes), shows platforms and null safety, and supports verified publishers.

dart pub get                 # resolve and download (creates pubspec.lock)
dart pub add http            # add a dependency
dart pub add --dev test
dart pub remove http
dart pub upgrade             # upgrade within constraints
dart pub upgrade --major-versions
dart pub outdated
dart pub deps
dart pub global activate dart_frog_cli
dart pub publish --dry-run
dart pub cache repair

30.3 Publishing a Package

Write a clear README, CHANGELOG, LICENSE, an example (example/), and docs (///). Follow semantic versioning. Run dart pub publish --dry-run and fix the warnings. Use automated publishing from GitHub Actions (OIDC).