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
addwrites it,getfetches it,upgrademoves 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 againAfter flutter pub add http, pubspec.yaml gains a line like this:
dependencies:
flutter:
sdk: flutter
http: ^1.2.0dependenciesship inside the app.dev_dependenciesare only for development, such as testing and linting tools.getreadspubspec.yamlandpubspec.lockand downloads what is needed. If a lock file exists, it keeps the locked versions.upgradeignores 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:
"I need a new package"
flutter pub add name. Writedev:nameinstead 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.
Which command adds http to pubspec.yaml and fetches it?
A teammate pulls your changes. Which command gives them the same package versions you have?
A mocking library is used only in tests. Where should it go?
You write a small command-line tool in pure Dart with no Flutter. Which command manages its packages?
Remember
addwrites it,getfetches it,upgrademoves it forward.- Match intent: new package, just cloned, want newer, no longer needed.
- Trap:
upgradewhen you meantget.
