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.dart file
  • 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 void keyword. This represents that the corresponding function doesn’t return any value. And that function is our main function.
  • Then we have the name of the function as main followed a pair of brackets/parentheses. This main() 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 is print(). 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.