Basic Program
In this tutorial, we’ll go through the process of writing, understanding, and running a simple Dart program that prints “Hello, World!” to the console.
Prerequisites
-
Ensure Dart is installed on your system. You can verify this by
running
dart --version
in your terminal or command prompt.
Creating Your First Dart Program
Step 1: Set Up Your Project
-
Create a New Directory:
-
Create a new folder for your Dart project. You can name it
anything, for example,
hello_dart
. -
Navigate into your new directory:
mkdir hello_dart cd hello_dart
-
Create a new folder for your Dart project. You can name it
anything, for example,
-
Create a Dart File:
-
Inside the directory, create a new file named
main.dart
. -
You can do this using a text editor, or by running the
following command in the terminal:
touch main.dart
-
Inside the directory, create a new file named
Step 2: Write the Dart Code
-
Open
main.dart
in Your Text Editor:-
Add the following Dart code to the file:
void main() { print('Hello, World!'); }
-
Add the following Dart code to the file:
LIVE VIEW{:target="_blank"}
-
This program starts with a
main
function, which is the entry point of many programming languages, including Dart. -
The
print()
function is used to output text to the console.
Step 3: Run Your Dart Program
-
Execute the Program:
- Open your terminal or command prompt.
-
Navigate to the directory containing your
main.dart
file. -
Run the program by typing:
dart run main.dart
-
View the Output:
-
If everything is set up correctly, you will see
Hello, World!
printed in the terminal.
-
If everything is set up correctly, you will see
Understanding the Code
-
void main()
: This declares the main function.void
indicates that this function does not return a value. -
print('Hello, World!')
: This line outputs the stringHello, World!
to the console.
Next Steps
After running your first program, you might want to explore more about Dart:
- Variables and Types: Learn about declaring and using variables in Dart.
-
Control Structures: Explore how to use
if
statements, loops, and other control structures. - Functions: Understand how to define and invoke functions.
- Classes and Objects: Dive into object-oriented programming with Dart.
Explore these topics by visiting the official Dart documentation.