๐ฆ Lesson 4: Packages & Libraries
Why Packages Matter
Imagine building a house where you have to craft every nail and brick yourself. Packages are like pre-built components - doors, windows, and fixtures you can just install!
โ Without Packages
- Copy-paste code everywhere
- Hard to update
- No version control
- Reinventing the wheel
โ With Packages
- Import once, use everywhere
- Easy updates via pub
- Semantic versioning
- 40,000+ packages on pub.dev
Today's Project: "Super Utils" Package Suite
We'll create a professional package ecosystem:
- ๐ฆ Core package with utility functions
- ๐ง CLI app that consumes the package
- ๐ Proper public API with exports
- ๐งช Test suite for the package
- ๐ Real pubspec.yaml configuration
Step 1
Understanding Package Structure
๐ Interactive Package Explorer
super_utils/
lib/
super_utils.dart
src/
string_utils.dart
math_utils.dart
date_utils.dart
test/
pubspec.yaml
README.md
๐ File Details
Click on a file to see its purpose and contents!
๐ Key Convention: lib/src/ is Private
Files in lib/src/ are conventionally private. Users of your package shouldn't import them directly. Instead, export them from the main library file.
import 'package:super_utils/super_utils.dart';
โ
Correct - Uses public API
import 'package:super_utils/src/string_utils.dart';
โ Wrong - Bypasses public API
Step 2
Mastering pubspec.yaml
๐๏ธ Interactive pubspec.yaml Builder
๐ Generated pubspec.yaml
name: super_utils
version: 1.0.0
description: A super collection of utility functions
environment:
sdk: ^3.12.0
dependencies:
http: ^1.3.0
intl: ^0.19.0
dev_dependencies:
test: ^1.24.0
lints: ^5.0.0
Step 3
Designing Public API with Exports
๐จ API Design Playground
Choose which files to include in your public API:
๐ Generated lib/super_utils.dart
library;
export 'src/string_utils.dart';
export 'src/math_utils.dart';
๐ก Best Practice: Only export files that form your public API.
Keep internal helpers private so you can change them without breaking users.
Step 4
Managing Dependencies
๐ Dependency Graph Visualizer
Your App
Click buttons to add dependencies and see the graph grow!
Step 5
Dart Workspaces (Modern 2026)
๐ข Multi-Package Workspace
๐ monorepo/
๐ pubspec.yaml (workspace root)
๐ super_utils/
๐ cli_app/
๐ web_service/
Root pubspec.yaml
name: _
publish_to: none
environment:
sdk: ^3.12.0
workspace:
- super_utils
- cli_app
- web_service
โจ Benefits:
- Single
pub getfor all packages - Shared dependency resolution
- Consistent versioning
- Easier refactoring across packages
๐
Build: Super Utils Package Suite
๐ String Utilities
๐งช Test Your Functions
๐ข Math Utilities
๐งช Test Your Functions
๐ Date Utilities
๐งช Test Your Functions
๐ Explore pub.dev
๐ฆ http
A composable, multi-platform, Future-based API for HTTP requests.
๐ฆ intl
Internationalization and localization facilities.
๐ฆ path
A string-based path manipulation library.
๐ฎ Package Master Quiz
๐ Score: 0/6
โก Streak: 0
โฑ Time: 0s
๐ Level: 1
Loading question...
๐
Perfect Score
โก Speed Demon
๐ฅ Unstoppable