Dart Playground & Online Execution
Dart Playground & Online Execution
One of the most beginner-friendly aspects of Dart is how easy it is to start writing and running code — even without installing anything. In this lesson, we explore DartPad (the official online editor) and the Dart CLI (command-line interface) for running code on your own machine.
DartPad — The Official Online Editor
DartPad is a free, browser-based Dart editor maintained by the Dart team at Google.
🌐 Visit: https://dartpad.dev
No account. No installation. Open the URL and start coding immediately.
Anatomy of the DartPad Interface
When you open DartPad, you'll see:
┌──────────────────────────────────┬──────────────────────┐
│ CODE EDITOR │ OUTPUT PANEL │
│ │ │
│ void main() { │ Hello, DartPad! │
│ print('Hello, DartPad!'); │ │
│ } │ │
│ │ │
├──────────────────────────────────┴──────────────────────┤
│ [ Run ▶ ] [ Format ] [ New Pad ] [ Samples ▼ ] │
└─────────────────────────────────────────────────────────┘
Key UI elements:
| Element | What it does |
|---|---|
| Code Editor | Where you write Dart code |
| Output Panel | Shows print() output and error messages |
| Run button | Executes your code |
| Format button | Formats code using dart format rules |
| Samples | Built-in example programs to explore |
| Flutter toggle | Switch between Dart-only and Flutter mode |
Running Your First Code in DartPad
- Open https://dartpad.dev
- Clear any existing code
- Type the following:
dartvoid main() { print('Hello from DartPad!'); print('The answer to everything is: ${6 * 7}'); }
- Click the Run ▶ button
- See the output in the right panel:
Hello from DartPad!
The answer to everything is: 42
DartPad Features in Detail
Instant Error Feedback
DartPad highlights errors as you type — before you even run the code:
dartvoid main() { print('Missing closing parenthesis' // ← red underline here }
Error panel shows:
Error: Expected ')' before this.
Built-in Code Samples
Click Samples in the top bar to access pre-built examples:
- Hello World
- Fibonacci
- Futures (async/await)
- HTTP requests
- Flutter widget examples
These are excellent starting points for exploration.
Flutter Mode
Toggle the Flutter switch in the top-right to write Flutter widget code and see a live visual preview right in the browser — no emulator needed!
Sharing Code with DartPad Links
One of DartPad's most useful features: shareable URLs.
After writing code, the URL in your browser updates automatically to encode your code. Copy and paste the URL to share with anyone.
Example use cases:
- Share a code snippet with a colleague for review
- Ask for help on Stack Overflow with a reproducible example
- Embed DartPad in course materials or blog posts
You can also embed DartPad in HTML pages using an <iframe>:
html<iframe src="https://dartpad.dev/embed-dart.html?id=YOUR_PAD_ID" width="100%" height="400px"> </iframe>
DartPad Limitations
| Limitation | Why it Matters |
|---|---|
| No file system | Can't read/write local files |
| Limited packages | Only dart: libraries + approved packages (no pub.dev packages) |
| Single file | Can't have multiple .dart files |
| No custom pub packages | http, json, some packages work; most don't |
| Internet required | Won't work offline |
| Not for production | Purely a learning/prototyping tool |
Dart CLI — Running Code Locally
Once the Dart SDK is installed, the dart command-line tool is your primary interface for running and building Dart programs.
dart run — Execute a Dart File
The most common command you'll use as a learner:
bash# Create a file echo 'void main() { print("Hello CLI!"); }' > hello.dart # Run it dart run hello.dart
Output:
Hello CLI!
You can also run the file directly if it has a main() function:
bashdart hello.dart
Tip: dart run hello.dart and dart hello.dart are equivalent for simple scripts.
dart compile exe — Compile to Native Executable
Dart can compile your program to a self-contained native binary — no Dart runtime needed on the target machine!
bash# Compile to an executable dart compile exe hello.dart -o hello # Run the compiled binary (Windows: hello.exe) ./hello # macOS/Linux hello.exe # Windows
Output:
Hello CLI!
Why compile?
- Much faster startup time
- Distribute to users without requiring Dart SDK
- Deploy to servers as standalone binary
Other compile targets:
bash# Compile to JavaScript (for web) dart compile js hello.dart -o hello.js # Compile to kernel snapshot (faster startup, still needs Dart VM) dart compile kernel hello.dart -o hello.dill # Compile to AOT snapshot (native, smaller than exe) dart compile aot-snapshot hello.dart -o hello.aot
dart analyze — Static Code Analysis
Run the Dart analyzer to catch bugs, type errors, and style issues without running the code:
bashdart analyze
Example output for a file with issues:
Analyzing project...
error - hello.dart:3:3 - The method 'prnt' isn't defined for the class 'Object'. Try correcting the name. - undefined_method
warning - hello.dart:5:7 - The value of the local variable 'x' isn't used. - unused_local_variable
2 issues found.
Best practice: Run dart analyze before committing code. Many CI/CD pipelines do this automatically.
dart format — Auto-Format Your Code
Dart has an official code formatter that applies a consistent style — no more debates about indentation!
bash# Format a single file (in-place) dart format hello.dart # Format all files in a directory dart format lib/ # Preview formatting without applying changes dart format --output=show hello.dart # Check if files need formatting (exit code 1 if yes) dart format --set-exit-if-changed .
Before formatting:
dartvoid main(){String name='Dart';print('Hello, '+name+'!');int x=42;if(x>0){print('positive');}}
After dart format:
dartvoid main() { String name = 'Dart'; print('Hello, ' + name + '!'); int x = 42; if (x > 0) { print('positive'); } }
Quick Testing Strategies
When learning Dart, use these strategies to test ideas quickly:
1. Single-file scripts
Create a temp file for quick experiments:
dart// scratch.dart void main() { // Try out anything here var list = [1, 2, 3, 4, 5]; var doubled = list.map((x) => x * 2).toList(); print(doubled); // [2, 4, 6, 8, 10] }
bashdart run scratch.dart
2. DartPad for quick API exploration
Paste a code snippet into DartPad, hit Run — done. No files, no terminal.
3. dart create for proper projects
When testing something bigger:
bash# Create a new Dart project with proper structure dart create my_project cd my_project dart run
4. REPL-style debugging with dart run
Add print() statements liberally while learning — it's the fastest feedback loop:
dartvoid main() { var x = 10; print('x is: $x'); // Checkpoint 1 x = x * 2; print('x doubled: $x'); // Checkpoint 2 var result = x + 5; print('result: $result'); // Checkpoint 3 }
Dart CLI Quick Reference
| Command | What It Does |
|---|---|
dart run file.dart | Run a Dart file |
dart compile exe file.dart | Compile to native executable |
dart compile js file.dart | Compile to JavaScript |
dart analyze | Run static analysis |
dart format file.dart | Format code |
dart create project_name | Create a new Dart project |
dart pub get | Download project dependencies |
dart test | Run tests |
dart --version | Show Dart version |
Summary
In this lesson, you learned:
- DartPad at dartpad.dev is a powerful browser-based editor with real-time error checking, sample code, and shareable links
dart runexecutes Dart files locally from the terminaldart compile exeproduces self-contained native binariesdart analyzecatches bugs and style issues staticallydart formatenforces consistent code style automatically- Quick testing strategies: scratch files, DartPad,
dart create
Next Up: Let's write our first real Dart program — and dissect every single part of "Hello World" to understand what's actually happening under the hood.