Lesson 8 min

Dart Playground & Online Execution

00:00 / 00:00

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:

ElementWhat it does
Code EditorWhere you write Dart code
Output PanelShows print() output and error messages
Run buttonExecutes your code
Format buttonFormats code using dart format rules
SamplesBuilt-in example programs to explore
Flutter toggleSwitch between Dart-only and Flutter mode

Running Your First Code in DartPad

  1. Open https://dartpad.dev
  2. Clear any existing code
  3. Type the following:
dart
void main() { print('Hello from DartPad!'); print('The answer to everything is: ${6 * 7}'); }
  1. Click the Run ▶ button
  2. 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:

dart
void 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

LimitationWhy it Matters
No file systemCan't read/write local files
Limited packagesOnly dart: libraries + approved packages (no pub.dev packages)
Single fileCan't have multiple .dart files
No custom pub packageshttp, json, some packages work; most don't
Internet requiredWon't work offline
Not for productionPurely 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:

bash
dart 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:

bash
dart 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:

dart
void main(){String name='Dart';print('Hello, '+name+'!');int x=42;if(x>0){print('positive');}}

After dart format:

dart
void 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] }
bash
dart 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:

dart
void 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

CommandWhat It Does
dart run file.dartRun a Dart file
dart compile exe file.dartCompile to native executable
dart compile js file.dartCompile to JavaScript
dart analyzeRun static analysis
dart format file.dartFormat code
dart create project_nameCreate a new Dart project
dart pub getDownload project dependencies
dart testRun tests
dart --versionShow 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 run executes Dart files locally from the terminal
  • dart compile exe produces self-contained native binaries
  • dart analyze catches bugs and style issues statically
  • dart format enforces 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.

WhatsApp