Our First Dart Program
Here's a quick test to run our first program in Dart.
Creating the hello.dart
- Open VS Code
- Create a folder named, “dart_programs”
- Create a
hello.dartfile - Paste this code
void main() {
print("Welcome to Dart!");
}
Running our hello.dart file
- Open the integrated VS Code terminal by pressing Ctrl + ` or by clicking on the Terminal tab in the title bar of VS Code > then New Terminal
- Type this
dart .\hello.dart
Expected Output
Welcome to Dart!
Understanding the hello.dart file
Let’s look at our hello.dart file!
void main() {
print('Welcome to Dart!');
}
- In the highlighted line of our code, we can see there is a
voidkeyword. This represents that the corresponding function doesn’t return any value. And that function is ourmainfunction. - Then we have the name of the function as
mainfollowed a pair of brackets/parentheses. Thismain()represents the entry-point of our program or the initial point of execution. - The curly braces define the
main()function’s body - what will the function contain?
ℹ️ Note
Everything inside the curly braces will eventually run when main() function is called!
- Inside the
main()function’s body is a statement which isprint(). This is an inbuilt function that prints the enclosed content inside the parentheses to the console. - Inside the
print()function’s parentheses, the argument ‘Welcome to Dart!’ is passed as a string.
ℹ️ Note
A string is a collection of characters.
Notice that the string is enclosed inside single quotes. Strings can be also enclosed inside double quotes.
But in Dart, we use single quotes more often to enclose our strings, unless we use single quotes in our actual argument,
e.g. "Alex's soup is ready to be served!"
- A semicolon character (
;) marks the end of the print statement.