What is pub.dev?
pub.dev is the official package repository for Dart and Flutter. It hosts thousands of community and official packages. The dart pub command manages packages — installing, updating, and removing dependencies.
pubspec.yaml
Every Dart project has a pubspec.yaml that declares metadata and dependencies:
name: my_dart_app
description: A sample Dart application
version: 1.0.0
environment:
sdk: '>=3.0.0 <4.0.0'
dependencies:
http: ^1.1.0
intl: ^0.18.0
path: ^1.8.0
equatable: ^2.0.5
dev_dependencies:
test: ^1.24.0
lints: ^3.0.0
Adding Packages
# Add a dependency
dart pub add http
# Add a dev dependency
dart pub add --dev test
# Get all dependencies
dart pub get
# Update all to latest compatible:
dart pub upgrade
# Upgrade to potentially-breaking latest:
dart pub upgrade --major-versions
# Remove a package:
dart pub remove http
Popular Packages
| Package | Purpose |
http | HTTP requests |
dio | Advanced HTTP with interceptors |
intl | Internationalization, date formatting |
path | Cross-platform file path manipulation |
json_serializable | Code-gen for JSON models |
freezed | Immutable classes, unions, copyWith |
equatable | Value equality for classes |
test | Dart testing framework |
mockito | Mocking for tests |
shelf | Dart HTTP server framework |
Creating Your Own Package
# Create a new package:
dart create --template=package my_package
# Package structure:
# my_package/
# lib/
# my_package.dart ← public API entry point
# src/
# feature_one.dart ← internal implementation
# test/
# my_package_test.dart
# pubspec.yaml
# README.md
# CHANGELOG.md
// lib/my_package.dart — export only public API
library my_package;
export 'src/string_utils.dart';
export 'src/math_utils.dart';
// lib/src/string_utils.dart
extension StringExtensions on String {
String capitalize() => isEmpty ? this : '$${this[0].toUpperCase()}$${substring(1)}';
}
🧠 Quiz
1. What file declares a Dart project's dependencies?
- A) package.json
- B) dart.yaml
- C) pubspec.yaml ✅
- D) dependencies.yaml
2. What does the ^ prefix mean in a version constraint like ^1.1.0?
- A) Exactly version 1.1.0
- B) Any version >= 1.1.0 and < 2.0.0 ✅
- C) Any version >= 1.1.0
- D) Beta version
Summary
pub.dev is Dart's package repository. Declare dependencies in pubspec.yaml and install with dart pub get. Use dart pub add <package> for quick installs. The ^ prefix means "compatible with this version". Create packages with dart create --template=package and expose your API via export in the library's entry point.