Rilasciato · in miglioramento
Guida a Dart · 4/6
Per ora questo capitolo è disponibile solo in inglese.
Dart code is organized into two units: libraries and packages. A library groups code and defines what is visible, while a package distributes libraries together with configuration and dependencies. This chapter covers import and export, the pub package manager and pubspec.yaml, the standard package layout, and publishing to pub.dev.
In Dart, every file is a library. Top-level declarations and members whose names start with an underscore (_) are visible only inside that library, so there is no private keyword. To use another library, bring it in with import.
| Form | Example | Meaning |
|---|---|---|
dart: | import 'dart:math'; | A core library that ships with the SDK |
package: | import 'package:http/http.dart'; | A dependency, or your own package's lib |
| Relative path | import 'src/parser.dart'; | Another file in the same package |
import 'dart:math';
import 'dart:convert' show jsonDecode, jsonEncode; // only these names
import 'package:path/path.dart' as p; // a prefix avoids name clashes
import 'src/internal.dart' hide debugHelper; // everything except one name
void main() {
print(sqrt(16)); // dart:math
print(jsonEncode({'ok': true})); // dart:convert
print(p.join('assets', 'images', 'logo.png')); // package:path
}show imports only the listed names and hide everything except them. An as prefix prevents name clashes. On the web, deferred as loads a library lazily.
A package usually keeps its implementation under lib/src and exports only the public parts from a main file directly under lib. Users then need to import just that one file.
// lib/shapes.dart: the package's main library
library;
export 'src/circle.dart';
export 'src/square.dart' show Square;
// src/geometry_utils.dart is not exported, so it stays an implementation detailBy convention, files in lib/src are private implementation. Other packages can technically import package:shapes/src/..., but it is discouraged and the analyzer warns about it.
pub is Dart's package manager, and is the public package repository. A package's name, version, supported SDK range and dependencies live in at the project root.
pubspec.yamlname: weather_cli
description: An example tool that shows the weather on the command line.
version: 1.0.0
publish_to: none # prevents accidental publishing to pub.dev
environment:
sdk: ^3.5.0 # dart create fills this in for your current SDK
dependencies:
http: ^1.2.0
path: ^1.9.0
dev_dependencies:
lints: ^5.0.0
test: ^1.25.0A caret (^) allows newer versions up to the next major one: ^1.2.0 means at least 1.2.0 and below 2.0.0. dependencies are needed at run time; dev_dependencies only during development. The versions above are examples; the commands below fill in the latest compatible ones.
dart pub add http # add to dependencies
dart pub add --dev test # add to dev_dependencies
dart pub remove http # remove a dependency
dart pub get # fetch what pubspec.yaml asks for
dart pub upgrade # upgrade to the newest allowed versions
dart pub outdated # list dependencies with newer versions
dart pub deps # print the dependency treedart pub get records the chosen versions in pubspec.lock. Commit it for apps and executables so everyone and CI use the same versions; for libraries, consumers resolve versions again, so it matters less. Packages are stored in a shared pub cache.
You can also depend on packages from a Git repository or a local path.
dependencies:
my_utils:
path: ../my_utils
shared_models:
git:
url: https://github.com/example/shared_models.git
ref: mainThe pub tool and pub.dev expect a conventional directory layout.
| Path | Contents |
|---|---|
lib/ | Library code exposed to other packages |
lib/src/ | Implementation details (private by convention) |
bin/ | Entry points run with dart run |
test/ | Tests (file names end in _test.dart) |
example/ | Usage examples (shown on the pub.dev Example tab) |
tool/ | Development scripts such as build or release helpers |
README.md, CHANGELOG.md, LICENSE | Introduction, change history, license |
Files outside lib (in bin, test and so on) import their own package's libraries with the package: form, for example import 'package:weather_cli/weather_cli.dart';.
For a public package, remove publish_to: none and provide a description, a repository or homepage, a LICENSE, a README.md and a CHANGELOG.md. Always run a dry run before publishing.
dart pub publish --dry-run # check for problems without uploading
dart pub publish # authenticate with a Google account and publishPublished versions cannot be deleted, and the same version number cannot be uploaded twice. A mistaken version can be retracted for a short period after publishing, but the usual fix is to publish a corrected new version. Follow semantic versioning and bump the major version for breaking changes. pub.dev scores packages on documentation, static analysis and platform support, so checking with dart analyze and dart doc beforehand pays off.
In Dart, each file is a library and names starting with _ stay hidden outside it. Control what you import with show, hide and as, and gather the public API in a main file with export. Declare dependencies in pubspec.yaml, manage them with dart pub add, get and upgrade, follow the standard layout, and publish to pub.dev with dart pub publish.
0 commenti
Accedi · Accedi per lasciare un commento.
Scrivi tu il primo commento.