Your First Dart Program
The anatomy of a Dart file — main(), statements, comments, and how the language reads top to bottom.
អាន 2 នាទី
Every standalone Dart program shares the same entrypoint: a top-level function called main. Create a file named hello.dart:
void main() {
print('Hello, world!');
}Run it with dart run hello.dart, and Dart prints Hello, world! to the terminal. There's no class wrapper, no public static void main(String[] args) ceremony — just a function the Dart runtime looks for and calls automatically when the program starts.
Statements and semicolons
Dart statements end with a semicolon, and code blocks are grouped with curly braces {} — familiar territory if you've used JavaScript, Java, or C#:
void main() {
print('Line one');
print('Line two');
int total = 2 + 2;
print(total);
}Each line above is a separate statement. Forgetting the semicolon is one of the most common early mistakes — the analyzer will flag it immediately rather than letting it fail silently.
Comments
Dart supports single-line, multi-line, and documentation comments:
// This is a single-line comment.
/*
This is a multi-line comment,
useful for longer explanations.
*/
/// This is a documentation comment — tools like `dart doc`
/// and IDE tooltips pick these up when they start with three slashes.
void greet() {
print('Hi!');
}The triple-slash /// form isn't just decoration: hover over a function that has one in VS Code, and the comment shows up as the tooltip. Get in the habit of using it on anything another developer (including future you) will call without reading its source.
Command-line arguments
main can optionally receive the arguments a program was run with, as a List<String>:
void main(List<String> arguments) {
if (arguments.isEmpty) {
print('No arguments provided.');
} else {
print('You passed: ${arguments.join(', ')}');
}
}Running dart run hello.dart Alice Bob prints You passed: Alice, Bob. Notice the ${...} inside the string — that's string interpolation, Dart's way of embedding expressions directly inside a string literal instead of concatenating pieces with +. You'll use it constantly, so it's worth internalizing now: '$name' for a bare variable, '${expression}' when you need to evaluate something more than a single identifier.
From here, every lesson builds on this same shape: a main function (or a function main calls) that you can run directly and see the result of immediately.