Sub topic 16
Libraries, Imports, and Packages
Assume you know basic Dart syntax, classes, and how to run a Dart program. This chapter explains how Dart code is split into libraries and how projects reuse code from the SDK or external packages.
Importing a library
A Dart file is a library. An import statement makes declarations from another library available in the current file. Imports normally appear before variables, functions, and classes.
Dart supports several URI schemes:
| URI form | Purpose | Example |
|---|---|---|
| `dart:` | Dart SDK library | `dart:math` |
| `package:` | A dependency or package library | `package:http/http.dart` |
| Relative path | Another file in the same project | `src/parser.dart` |
This program imports two SDK libraries. The as keyword gives dart:math a prefix, while show limits the names imported from dart:convert.
import 'dart:convert' show jsonEncode;
import 'dart:math' as math;
void main() {
final point = math.Point(3, 4);
final data = {
'x': point.x,
'y': point.y,
'length': math.sqrt(point.x * point.x + point.y * point.y),
};
print(jsonEncode(data));
}A prefix prevents name collisions. For example, if two libraries both define Logger, you can import them with different prefixes and write first.Logger or second.Logger.
Use show to import only selected declarations. Use hide to exclude declarations that conflict with local code.
import 'dart:math' hide Point;Creating and exposing project libraries
A project can organize code into folders. A common package layout places public libraries in lib/ and implementation details in lib/src/.
my_app/
lib/
weather.dart
src/
forecast.dart
bin/
main.dart
pubspec.yamlA public library can re-export selected implementation libraries:
// lib/weather.dart
export 'src/forecast.dart';Other files then import the stable public entry point:
import 'package:my_app/weather.dart';This keeps callers independent from the internal src directory. You can change the implementation without changing every import in the application.
Names that begin with an underscore are private to their library. For example, _parseTemperature cannot normally be used by a different library. Use this convention for implementation details rather than making every helper part of the public API.
Packages and dependencies
A package is a reusable Dart project. The pubspec.yaml file declares its name, SDK constraints, and dependencies. The Dart package manager, called pub, reads this file and downloads compatible versions.
A dependency entry might look like this:
name: weather_app
environment:
sdk: ^3.0.0
dependencies:
http: ^1.0.0After adding a dependency, run dart pub get. Then import its public library with a package: URI:
import 'package:http/http.dart' as http;The caret allows compatible releases according to Dart's version rules. Check a package's documentation before choosing a version, especially when the package requires a particular Dart SDK version.
Exercise
You need sqrt from dart:math, but your file also has another declaration named sqrt. How can you avoid the collision?
Show answer
Import dart:math with a prefix, such as import 'dart:math' as math;, and call math.sqrt(...).