๐Ÿ“ฆ Lesson 4: Packages & Libraries

โฑ 25-35 min ๐Ÿ“Š Intermediate ๐Ÿ—๏ธ Project-Based ๐Ÿ”ฅ Essential 2026 ๐Ÿ”— Official Source
๐Ÿค”

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 get for 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.

โญ 1000+ likes | ๐Ÿ”„ Updated weekly

๐Ÿ“ฆ intl

Internationalization and localization facilities.

โญ 900+ likes | ๐Ÿ”„ Updated weekly

๐Ÿ“ฆ path

A string-based path manipulation library.

โญ 800+ likes | ๐Ÿ”„ Updated weekly

๐ŸŽฎ Package Master Quiz

๐Ÿ† Score: 0/6
โšก Streak: 0
โฑ Time: 0s
๐Ÿ”“ Level: 1

Loading question...

๐Ÿ… Perfect Score โšก Speed Demon ๐Ÿ”ฅ Unstoppable