CodeBlanks Logo Icon CodeBlanks

pub

Learn the pub package manager.

The pub package manager

pub is Dart's package manager. With dart pub commands you add, update and publish packages. Packages come from the pub.dev repository.

Common commands: dart pub get fetches dependencies, dart pub add adds a package, dart pub upgrade updates. dart pub publish publishes your package.

pub reads the pubspec.yaml file. This file holds the package name, version and dependencies.

pub commands

pub commands do different jobs. dart pub get fetches dependencies, dart pub add package adds a dependency, dart pub upgrade raises versions.

dart pub outdated lists packages that need updates. dart pub publish sends your package to pub.dev.

Commands operate on pubspec.yaml. dart pub get runs after every dependency change.

Practice dart:math and max

Import the math library with import 'dart:math';. In main print the result of max(7, 3) with print.

Starter code

main.dart
// Import the dart:math library

void main() {
  // print the max of 7 and 3
}

Sample solution

main.dart
import 'dart:math';

void main() {
  print(max(7, 3));
}

Expected output

7

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does dart pub get do?

  • Fetches dependencies Correct answer
  • Publishes the package
  • Deletes the package
  • Runs tests

Explanation: dart pub get fetches dependencies.

2 The dart pub get command

Question
dart ____ get
Answer
dart pub get

Explanation: the command is written 'dart pub get'.

3 Match the commands

Term Meaning
dart pub get Fetches dependencies
dart pub add Adds a package
dart pub upgrade Upgrades versions
dart pub publish Publishes to pub.dev

All lessons in Intermediate