Dart Packages And Libraries
When the project scale grows, splitting code into multiple files and modules becomes essential.
This chapter introduces Dart's package management system pub, the dependency configuration file pubspec.yaml, import/export syntax, and how to create custom libraries.
* * *
## pubspec.yaml Configuration
pubspec.yaml is the core configuration file for each Dart project, declaring project metadata and dependencies.
A typical pubspec.yaml file structure is as follows:
## Example
# File path: pubspec.yaml
name: my_dart_app # Project name (required, lowercase + underscore)
description: A Dart sample project # Project description
version: 1.0.0 # Version number
# publish_to: none # If you don't want to publish to pub.dev, uncomment this line
environment:
sdk: '>=3.0.0 <4.0.0' # Dart SDK version range
dependencies:
# Third-party packages required at runtime
http: ^1.1.0 # HTTP client
path: ^1.8.0 # Path utility
dev_dependencies:
# Dependencies needed only during development (testing, linting, etc.)
test: ^1.24.0 # Testing framework
lints: ^2.0.0 # Official lint rules
# Optional: Executable entry point
# executables:
# my_app: main
Version number notation guide:
| Notation | Meaning | Example |
| --- | --- | --- |
| ^1.1.0 | Compatible with 1.1.0 to 2.0.0 (exclusive) | Most common, recommended |
| 1.1.0 | Exact version | Less flexible |
| >=1.1.0 <1.5.0 | Version range | Used when precise control is needed |
| any | Any version | Not recommended |
> The ^ symbol (caret) is Dart's default version constraint method. ^1.1.0 is equivalent to >=1.1.0 <2.0.0. This
YouTip