Dart: Hello World!
What is Dart?
Dart is an object-oriented programming language released by Google to help build modern web, mobile, and desktop applications. It has tons of awesome features that you will learn throughout this course.
Your first Dart project
You can easily create and run your first Dart application with VS Code.
Press
CMD + SHIFT + P
on Mac orCTRL + SHIFT + P
on WindowsChoose
Dart: New Project
Select
Simple Console Application
Select a folder where you want the course to be located
Name your project:
dart_application_1
After completing these steps, you should see a whole Dart project set up for you in VS Code:
If you're a complete beginner, it's important to note that a “project” is just a folder on your computer where all the files Dart needs to create your program live. There are many different files, all with different roles and purposes - don't worry if it seems confusing at first.
Where it all begins 🙂
Dart and many other languages have a main() function that is the program's entry point. In technical terms, this is where your program begins executing. Simply put, it's where everything in your app begins.
You can find this function in the dart_application_1.dart
file in your /bin
directory.
Let's erase everything in this file and create our main function.
The main() function is written in curly brackets ({}). Flutter will run the code in these brackets once your app starts.
void main(){
}
let’s start by printing something to the console with a print statement.
void main(){
print("Hello World"); //add this line
}
You can use (or call) the function print
to print anything. print
takes in an object as a parameter (The item that goes inside the parentheses) and prints a String (text) representation of that object. Simply put, it tries to print whatever you put in the parentheses!
In this case, we want to print Hello World, so we write “Hello World” inside print. The quotes are essential - we'll explain why in the coming lesson.
🤔 Try removing the quotes and see what happens. What does your Terminal say?
Let's run the program. 🚀
There are two main ways to run your program:
Press F5 and VS Code will automatically run your program with the debugger.
💡 Debugger: A program that assists in detecting and correcting errors in other computer programs.
💡 Terminal/Console: A terminal is where you can give your program's commands as text, and see what output (or errors) your programs create.
A more advanced way to start your app is to use your computer's Terminal. To do this, go to the top bar of your VS Code window and press ‘Terminal’ then ‘Create New Terminal,’ which should open a terminal on the bottom of your VS Code window. There, you can type
dart run,
Make sure the Terminal is already inside your project folder. If it is not, navigate using the
cd
commands. You can learn more about Terminals here❗ The “Terminal” and “Debug Console” are Different! The “Terminal is your computer's Terminal (CMD on Windows, Terminal on Mac). The DEBUG CONSOLE is where the Dart debugger prints output and errors
Voila! You should now see a message popping up displaying in your console: “Hello World.”
Learn Flutter where you code: Visit sideguide.dev/courses/flutter
Takeaways
You can create a Dart project by pressing
CTRL+SHIT/CMD+P
and selectingDart: New Project
Debug Console and Terminal are 2 different things
You can run your program using the Debug play button in VS Code or typing
dart run
in the terminal