Skip to the workspace
Pebellslearn by building
Package Managers  ›  Lesson

flutter pub: Add, Get, Upgrade

flutter pub / dart pub 12 minThe pub commands add and remove dependencies in pubspec.yaml, fetch them with get, and move to newer allowed versions with upgrade, using flutter pub in Flutter projects and dart pub in pure Dart ones.

flutter / flutter pub: Add, Get, Upgrade

Explore each scene at your own pace.

01 · The rule

The rule

flutter pub: Add, Get, Upgrade

You have chosen the http package so Sprout can fetch the local weather and skip watering outdoor plants on rainy days. Now it has to get into the project. You could edit pubspec.yaml by hand, but the pub commands do it correctly, including the version, in one line.

The rule

add writes it, get fetches it, upgrade moves it forward.

flutter pub add http              # add to dependencies and fetch
flutter pub add dev:mocktail      # add to dev_dependencies (tests only)
flutter pub get                   # fetch exactly what pubspec.lock says
flutter pub outdated              # show newer versions available
flutter pub upgrade               # move to the newest allowed versions
flutter pub remove http           # take it out again

After flutter pub add http, pubspec.yaml gains a line like this:

dependencies:
  flutter:
    sdk: flutter
  http: ^1.2.0
  • dependencies ship inside the app. dev_dependencies are only for development, such as testing and linting tools.
  • get reads pubspec.yaml and pubspec.lock and downloads what is needed. If a lock file exists, it keeps the locked versions.
  • upgrade ignores the old locked versions, picks the newest ones your constraints allow, and rewrites the lock file.

flutter pub or dart pub? They are the same tool. Use flutter pub inside a Flutter project, because it runs with the Flutter SDK and knows about Flutter packages. Use dart pub for a pure Dart project, such as a command-line script, that does not depend on Flutter.

Then import and use it:

import 'package:http/http.dart' as http;
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.