Veröffentlicht · wird verbessert
Dart-Anleitung · 1/6
Dieses Kapitel ist vorerst nur auf Englisch verfügbar.
Dart is a client-optimized, general-purpose language created by Google. You can use it to build command-line tools, servers and web apps, and it is best known as the language behind Flutter. In this chapter you will install the Dart SDK, create and run a first project, try code in the browser with DartPad, and set up your editor.
The Dart SDK bundles the compiler, runtime, package manager (pub), analyzer, formatter and test runner behind a single dart command. There are two common ways to get it.
A package manager is usually the simplest route. On macOS use Homebrew, on Windows use Chocolatey, and on Debian or Ubuntu use the apt repository provided by Google. You can also download an archive, extract it and add its bin directory to your PATH.
# macOS (Homebrew)
brew tap dart-lang/dart
brew install dart
# Windows (Chocolatey, from an elevated terminal)
choco install dart-sdk
# Verify the installation
dart --versionIf dart --version prints an SDK version, you are ready. If you installed Flutter, the dart command works as long as flutter/bin is on your PATH. When both are installed, run which dart (or where dart on Windows) to see which one comes first and avoid version confusion.
dart create generates a new project from a template. The default template is a command-line app (console); with -t you can pick others such as package (a reusable library), server-shelf (a small HTTP server), cli (a command-line tool with argument parsing) or web.
dart create hello_dart
cd hello_dart
dart runThe generated project looks like this.
| Path | Purpose |
|---|---|
bin/hello_dart.dart | Entry point with the main function |
lib/hello_dart.dart | The project's library code |
test/hello_dart_test.dart | Tests written with package:test |
pubspec.yaml | Package name, SDK constraint, dependencies |
analysis_options.yaml | Static analysis (lint) settings |
Open bin/hello_dart.dart and write your first program. A Dart program starts at the top-level main function, which can receive command-line arguments as a List<String>.
void main(List<String> arguments) {
final name = arguments.isEmpty ? 'Dart' : arguments.first;
print('Hello, $name!');
}dart run finds the entry point under bin and runs it, and you can pass arguments after it. To run a single file, give its path directly.
dart run # runs bin/<package_name>.dart
dart run bin/hello_dart.dart developer
dart compile exe bin/hello_dart.dart -o hello
./hellodart run uses the JIT (just-in-time) compiler for a fast development loop, while dart compile exe produces a self-contained native executable with the AOT (ahead-of-time) compiler. The executable runs on machines of the same operating system and architecture without a Dart SDK. You can also target JavaScript with dart compile js and WebAssembly with dart compile wasm.
| Command | What it does |
|---|---|
dart analyze | Reports type errors and lint warnings |
dart format . | Formats code in the official style |
dart fix --apply | Applies automated fixes suggested by the analyzer |
dart test | Runs the tests in the test directory |
dart pub get | Fetches the dependencies in pubspec.yaml |
dart doc | Generates API documentation |
Make dart format and dart analyze a habit before every commit. The Dart community accepts the official formatter's output as-is, so there is very little debate about indentation or line breaks.
To taste Dart without installing anything, open DartPad. You write code in the browser and run it immediately, with access to the core libraries, a set of popular packages, and even Flutter UI rendering. It is handy for checking short examples, experimenting with syntax, or sharing a reproduction with someone else. Code that relies on dart:io, such as file system or process access, cannot run in the browser.
The Dart team maintains official plugins for VS Code and the IntelliJ family (IntelliJ IDEA and Android Studio).
Both editors use the Dart analysis server, so they offer the same code completion, error highlighting, quick fixes, renaming and format-on-save. In VS Code, enable "editor.formatOnSave": true to get dart format output every time you save. Static analysis rules are tuned in the project's analysis_options.yaml.
include: package:lints/recommended.yaml
linter:
rules:
prefer_single_quotes: true
avoid_print: falseProjects created by dart create already include the recommended rules from package:lints. Enable or disable rules to match your team's conventions.
You can get the Dart SDK on its own or as part of Flutter, and a single dart command handles project creation (dart create), running (dart run), compiling (dart compile), analysis, formatting and tests. Before installing, DartPad lets you experiment right away; for real work, pair the SDK with the Dart plugin for VS Code or IntelliJ. The next chapter covers the basic syntax: variables, functions and control flow.
0 Kommentare
Anmelden · Melde dich an, um einen Kommentar zu schreiben.
Schreib den ersten Kommentar.