Your First Dart Program
Every Dart application, whether a CLI script, a server, or a Flutter app under the hood, needs exactly one top-level main() function as its entry point; this is where execution begins when you run the program. The simplest possible Dart program is three lines: void main() { print('Hello, World!'); }. The void return type means main() doesn't hand back a value to the operating system, print() is a top-level function from dart:core (automatically imported into every Dart file, no import statement needed) that writes a line to standard output, and the whole file can be saved as any .dart filename, though CLI projects conventionally name the entry file after the package inside a bin/ folder.
Cricket analogy: Dart requiring exactly one main() entry point is like a match requiring exactly one official start-of-play signal from the umpire; no matter how many practice drills happened beforehand, the actual game begins at one defined moment.
Anatomy of a Dart Program
Beyond the bare minimum, a realistic Dart file typically starts with import statements (for dart:core extras like dart:math, or third-party packages from pub.dev), followed by top-level declarations — functions, classes, or constants — and finally the main() function that ties them together by calling into that code. Dart executes statements inside main() sequentially, top to bottom, just like most imperative languages, but functions and classes defined elsewhere in the file can be referenced from main() regardless of whether they're declared above or below it, because Dart resolves top-level declarations before execution starts. Command-line arguments passed when running the program arrive as List<String> arguments if you declare main() as void main(List<String> arguments).
Cricket analogy: Dart resolving top-level declarations before execution starts is like a squad list being finalized before the toss, every player is registered and available for selection regardless of the order they appear on the printed sheet.
Running Your Program
With the Dart SDK installed, save your code as hello.dart and run it from the terminal with dart run hello.dart, which JIT-compiles and executes the file in one step, ideal for quick iteration. For a proper CLI project created with dart create my_app, the entry file lives at bin/my_app.dart, and you run it with dart run from the project root, which automatically finds that entry point. When you're ready to ship a standalone executable, dart compile exe bin/my_app.dart AOT-compiles it into a native binary that runs without needing the Dart SDK installed on the target machine at all, useful for distributing a CLI tool to users who don't have Dart set up.
Cricket analogy: Using dart run for quick iteration versus dart compile exe for a final binary is like a net session versus the actual televised match, one is fast and flexible for practice, the other is the polished, ready-for-broadcast version.
Trying Dart with DartPad
DartPad, hosted at dartpad.dev, is an in-browser Dart editor and compiler that requires no installation at all — you type Dart (or even a minimal Flutter widget) directly into the page and click Run to see console output or a rendered UI immediately, powered by a server-side compiler rather than anything installed on your machine. It's the fastest way to experiment with a language feature, test a small snippet from documentation, or follow along with a tutorial without touching a terminal, though it isn't a replacement for a real project once you're building something with multiple files, external packages, or platform-specific code that needs a local SDK and editor.
Cricket analogy: DartPad is like a stadium's indoor practice nets available year-round with no booking required, perfect for quickly testing a shot, but it's not where the real, full-scale match with all its context actually gets played.
// hello.dart
void main(List<String> arguments) {
print('Hello, World!');
if (arguments.isNotEmpty) {
print('You passed ${arguments.length} argument(s):');
for (final arg in arguments) {
print(' - $arg');
}
} else {
print('Try running: dart run hello.dart Alice Bob');
}
}You can format any Dart file to the standard style automatically with dart format hello.dart, and catch style/type issues before running with dart analyze. Both ship with the SDK and are commonly wired into editor save-on-format settings.
- Every Dart program requires exactly one top-level
main()function as its execution entry point. print()is available with no import needed, sincedart:coreis automatically imported into every Dart file.- Dart resolves all top-level declarations before execution starts, so
main()can reference functions or classes defined anywhere in the file. dart run file.dartJIT-compiles and runs immediately, ideal for quick iteration during development.dart compile exeAOT-compiles a program into a standalone native binary that runs without the Dart SDK installed.- DartPad (dartpad.dev) offers a zero-install, in-browser environment for quickly testing Dart or small Flutter snippets.
dart formatanddart analyzeare built-in SDK tools for enforcing style and catching issues before running code.
Practice what you learned
1. What is required in every Dart program for it to run?
2. Why can you call print() in Dart without writing an import statement?
3. What is the key difference between `dart run` and `dart compile exe`?
4. How do command-line arguments become available inside main()?
5. What is DartPad primarily used for?
Was this page helpful?
You May Also Like
What Is Dart?
An introduction to Dart, the open-source, client-optimized language Google created for building fast, structured apps across mobile, web, desktop, and server targets.
Installing Dart and the SDK
How to install the Dart SDK on Windows, macOS, and Linux, verify the install, and understand what tools like dart, dartfmt, and pub give you.
Dart Variables and Types
How Dart's var, final, and const declarations work, an overview of Dart's built-in types, and how type inference lets Dart stay statically typed while looking concise.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics