Skip to content

Beginner · Async Dart and packages · topic 12 of 50 · 25 min

Packages and pub

Explanation

pub is Dart's package manager and pub.dev is the public package site. pubspec.yaml lists your project's name, SDK constraint, dependencies and dev_dependencies (and, in Flutter, assets and fonts). Add packages with dart pub add or flutter pub add, which update pubspec.yaml and run pub get. A caret version like ^1.2.0 allows compatible updates; pubspec.lock records exact versions. Choose packages by quality signals, not popularity alone.

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

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/
YAML uses indentation (spaces, not tabs) to show structure. Version numbers here are examples; use what pub adds.

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

bash
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 versions
pub add edits pubspec.yaml and downloads the package.
dart
import '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
}
Import a package with package:name/file.dart. "as http" adds a prefix to avoid name clashes.

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.devWhy it matters
Likes and downloadsPopularity and community use
Pub pointsAutomated checks: documentation, platform support, static analysis, up-to-date dependencies
Verified publisherThe publisher's identity is verified (for example flutter.dev, dart.dev or a company domain)
Last published date and issuesIs it maintained? Are bugs answered?
PlatformsDoes it support Android, iOS, web, desktop as you need?
LicenceCan you use it in your (commercial) app?
Think of it like this: Choosing a package is like choosing a contractor for your house. Ratings (likes) help, but you also check their registration (verified publisher), whether they are still working (recent updates), and whether they handle the kind of job you need (platform support).

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

pub.dev

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.

dart
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
}
One formatter, used across the app.

Practice (0/3 done)

Task 1 · Question

Which versions does http: ^1.2.0 allow?

Task 2 · Question

Where should a test-only tool like build_runner be listed?

Task 3 · Practice

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

Question 1

1.What does pubspec.lock store?

Question 2

2.Which command downloads the packages listed in pubspec.yaml?

Question 3

3.What does a verified publisher badge mean?

Question 4

4.Why does YAML indentation matter?

Read more (free)

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.

From Project structure and hot reload

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.

From Images, icons, fonts and themes

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.

From Semantic versioning, tags and releases

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.

From Packages, crates and modules

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

  1. This is the last topic of “Async Dart and packages”. The module exam comes next.
  2. Module 3 exam: Async Dart and packages · 13 questions · 25 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Flutter setup and your first app. 50 topics left in the course before the final exam.

← Previous: Isolates for heavy work