Lesson 2 / 25

The Dart SDK, Projects and pubspec.yaml

Create, run and manage Dart projects with the dart command-line tool.

One tool for everything

Install the Dart SDK on its own or get it bundled with Flutter. The single dart command covers the whole workflow: dart create scaffolds a project (console app, package, server or web templates), dart run runs it, dart test runs tests, dart analyze reports static analysis issues, dart format formats code, dart fix --apply applies automated fixes, dart pub add <package> adds dependencies and dart compile exe builds a native executable. A project is described by pubspec.yaml: the package name, the Dart SDK constraint (sdk: ^3.5.0), dependencies and dev_dependencies from pub.dev with version constraints, and other metadata. pubspec.lock records exact resolved versions; commit it for applications (and for packages it is usually not committed). Conventional layout: bin/ for entry points, lib/ for library code (with lib/src/ for private implementation), test/ for tests. VS Code and IntelliJ/Android Studio have excellent Dart plugins, and DartPad lets you run Dart and Flutter snippets in the browser.

Creating and running a console project

pubspec.yaml declares the SDK range and dependencies.

dart create -t console order_tools
cd order_tools
dart pub add http
dart pub add --dev test lints
dart run
dart test
dart analyze
dart format .
dart compile exe bin/order_tools.dart -o build/order_tools

# pubspec.yaml (excerpt)
# name: order_tools
# environment:
#   sdk: ^3.5.0
# dependencies:
#   http: ^1.2.0
# dev_dependencies:
#   lints: ^5.0.0
#   test: ^1.25.0

A Swiss Army knife

Where other ecosystems need separate tools for running, testing, formatting and packaging, dart is one Swiss Army knife with a blade for each job, all the same version and working together.

Quick check: Which file declares a Dart package's dependencies and SDK constraint?

  • package.json
  • build.gradle
  • Cargo.toml
  • pubspec.yaml
Answer

pubspec.yaml — pubspec.yaml is the manifest for Dart and Flutter packages.