Beginner · Async Dart and packages · topic 12 of 50 · 25 min
Packages and pub
Explanation
Why packages?
You should not write an HTTP client, a date formatter or a PDF generator from scratch. A package is reusable code published for others: Flutter apps commonly use http for networking, provider or flutter_bloc for state management, shared_preferences for settings, and many more. pub.dev hosts tens of thousands of them.
pubspec.yaml
name: chai_shop
description: Orders and sales for a tea stall.
version: 1.0.0+1
environment:
sdk: ^3.5.0 # Dart SDK versions this project supports
dependencies:
flutter:
sdk: flutter
http: ^1.2.0 # used in the app
intl: ^0.19.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^5.0.0 # only needed while developing
flutter:
uses-material-design: true
assets:
- assets/images/dependencies are packages your app uses at run time. dev_dependencies are only for development, like test tools, linters and code generators. They are not shipped in your app.
The version line 1.0.0+1 is your app's own version: 1.0.0 is the name users see, and the number after + is the build number that must increase for every store upload (module 11).
Adding a package
flutter pub add http # in a Flutter project
dart pub add intl # in a pure Dart project
flutter pub add --dev build_runner
flutter pub get # download everything listed in pubspec.yaml
flutter pub outdated # see which packages have newer versionsimport 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
void main() {
print(NumberFormat.currency(locale: 'en_IN', symbol: '₹').format(1234567.5)); // ₹12,34,567.50
}Versions: caret and lock file
Packages follow semantic versioning: MAJOR.MINOR.PATCH. A major change may break your code. ^1.2.0 (caret syntax) means "1.2.0 or any later version below 2.0.0". When you run pub get, pub picks versions that satisfy every constraint and writes the exact choices into pubspec.lock. Commit pubspec.lock for apps, so every developer and the build server use the same versions.
To update packages, run flutter pub upgrade (within your constraints) or change the constraint in pubspec.yaml for a new major version, then read the package's changelog for breaking changes and run your tests.
Choosing a good package
| Signal on pub.dev | Why it matters |
|---|---|
| Likes and downloads | Popularity and community use |
| Pub points | Automated checks: documentation, platform support, static analysis, up-to-date dependencies |
| Verified publisher | The publisher's identity is verified (for example flutter.dev, dart.dev or a company domain) |
| Last published date and issues | Is it maintained? Are bugs answered? |
| Platforms | Does it support Android, iOS, web, desktop as you need? |
| Licence | Can you use it in your (commercial) app? |
Common mistakes
- Tabs or wrong indentation in pubspec.yaml: "Error on line 12". YAML needs consistent spaces.
- Adding a package for every small task. Each dependency is code you must trust and update.
- Choosing an abandoned package because a tutorial used it. Check the last publish date.
- Editing pubspec.lock by hand. Change pubspec.yaml and let pub update the lock file.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Packages and pub · 1/6
Packages are reusable code from pub dot dev. Do not rebuild what already exists.
Prefer a video by a teacher? Find free videos on “flutter pubspec yaml packages pub dev how to choose package” on YouTube ↗
In real life: Formatting money the Indian way
A billing app must show amounts as ₹12,34,567.50 (Indian digit grouping), not 1,234,567.50. Instead of writing grouping logic by hand, the developer adds the intl package, a well-maintained package from the Dart team, and uses NumberFormat with the en_IN locale everywhere amounts are shown.
import 'package:intl/intl.dart';
final _inr = NumberFormat.currency(locale: 'en_IN', symbol: '₹', decimalDigits: 2);
String rupees(num amount) => _inr.format(amount);
void main() {
print(rupees(99)); // ₹99.00
print(rupees(1500000)); // ₹15,00,000.00
}Practice (0/3 done)
Which versions does http: ^1.2.0 allow?
Where should a test-only tool like build_runner be listed?
Create a Dart console project (dart create money_tool), add the intl package with dart pub add, and write a program that prints a list of five amounts in Indian rupee format and today's date as "19 Oct 2026" style. Then open the intl page on pub.dev and note its publisher, pub points and supported platforms.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- StreamsAsync Dart and packages · same module
- Futures and async/awaitAsync Dart and packages · same module
- Project structure and hot reloadFlutter setup and your first app
- Images, icons, fonts and themesLayouts
- Semantic versioning, tags and releasesDevOps: Git, Docker, Kubernetes and CI/CD · Git in depth
- Packages, crates and modulesRust Programming · Modules, crates, traits and generics
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What does pubspec.lock store?
Answer: The exact versions of every package chosen
Commit it for apps.
Which command downloads the packages listed in pubspec.yaml?
Answer: flutter pub get
pub get resolves and downloads dependencies.
What does a verified publisher badge mean?
Answer: The publisher's identity has been verified
It is a trust signal, not a guarantee.
Why does YAML indentation matter?
Answer: Indentation defines the structure; wrong spaces break parsing
Use consistent spaces.
What is analysis_options.yaml for?Related
Answer: Lint rules and static analysis settings
It configures the analyser.
What must you do after adding a new font in pubspec.yaml?Related
Answer: A full restart (hot reload is not enough)
Assets are bundled at build time.
package.json has "express": "~4.18.2". Which version may npm install?Related
Answer: 4.18.5
The tilde allows newer patches of the same MAJOR.MINOR only: at least 4.18.2 and below 4.19.0.
Why commit Cargo.lock for an application?Related
Answer: It pins exact dependency versions so builds are repeatable
Cargo.lock records the exact versions used.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- This is the last topic of “Async Dart and packages”. The module exam comes next.
- Module 3 exam: Async Dart and packages · 13 questions · 25 min, without help. 4 topics of this module still to finish first.
- Then the next module, Flutter setup and your first app. 50 topics left in the course before the final exam.