Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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 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;

The shortcut

Match the command to your intent:

Picking the right pub commandStep 1 of 4
  1. "I need a new package"

    flutter pub add name. Write dev:name instead if only tests or tools use it.

The trap is running upgrade when you only meant get. It can quietly move several packages to newer versions and change behaviour on a day you did not plan to.

Drills

Answer fast — trust the rule.

quick drill

Which command adds http to pubspec.yaml and fetches it?

quick drill

A teammate pulls your changes. Which command gives them the same package versions you have?

quick drill

A mocking library is used only in tests. Where should it go?

quick drill

You write a small command-line tool in pure Dart with no Flutter. Which command manages its packages?

Remember

  • add writes it, get fetches it, upgrade moves it forward.
  • Match intent: new package, just cloned, want newer, no longer needed.
  • Trap: upgrade when you meant get.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainAdd http to Sprout

Sprout needs the http package to fetch weather data, and the mocktail package only for tests. Give the commands you would run, describe what each changes in pubspec.yaml, and explain when you would use get versus upgrade.