Your first Dart program
Welcome to Dart! In this chapter, you'll ensure your setup is complete, and then work through creating your first Dart program. This chapter starts simple but moves fast!
Prerequisites
#Before you begin this chapter, ensure you:
- Installed the Dart SDK.
- Reviewed the Dart overview (if you're new to Dart).
Task 1: Confirm your Dart setup
#First, make sure Dart is ready to go on your system by following these steps.
Open a terminal (or command prompt).
Run the following command to check your Dart SDK version:
bashdart --version
Make sure that you see output similar to this (the version numbers might be different):
bashDart SDK version: 3.8.0 (stable) (None) on "linux_x64"
If you see an error like "command not found," refer to the Dart installation guide to set up your environment.
Task 2: Create a new Dart project
#Now, create your first Dart command-line application.
In the same terminal, create a new directory called
dartpedia
to hold your project. Then switch into that directory.bashmkdir dartpedia cd dartpedia
Run the following command:
bashdart create cli
The
dart create
command generates a basic Dart project named "cli" (for Command Line Interface). It sets up the essential files and directories you need.You should see output similar to this, confirming the project creation:
bashCreating cli using template console... .gitignore analysis_options.yaml CHANGELOG.md pubspec.yaml README.md bin/cli.dart lib/cli.dart test/cli_test.dart Running pub get... 1.2s Resolving dependencies... Downloading packages... Changed 49 dependencies! 1 package has newer versions incompatible with dependency constraints. Try `dart pub outdated` for more information. Created project cli in cli! In order to get started, run the following commands: cd cli dart run
Task 3: Run your first Dart program
#Next, run your program to test it out.
In the terminal, navigate into your new project directory:
bashcd cli
Run the default application:
bashdart run
This command tells Dart to execute your program.
You should see the following output:
bashBuilding package executable... Built cli:cli. Hello world: 42!
Congratulations! You've successfully run your first Dart program!
Task 4: Make your first code change
#Next, modify the code that generated Hello world: 42!
.
In a code editor, open the
bin/cli.dart
file.The
bin/
directory is where your executable code lives.cli.dart
is the entry point of your application.Inside, you'll see the
main
function. Every Dart program starts executing from itsmain
function.Check to make sure that your
bin/cli.dart
looks like this:bin/cli.dartdartimport 'package:cli/cli.dart' as cli; void main(List<String> arguments) { print('Hello world: ${cli.calculate()}!'); }
Simplify the output for now. Comment out the first line (you don't need this import statement), and change the
print
statement to display a simple greeting:bin/cli.dartdart// import 'package:cli/cli.dart' as cli; void main(List<String> arguments) { print('Hello, Dart!'); // Change this line }
Save your file. Then in the terminal, run your program again:
bashdart run
Check to make sure that you see the following:
bashBuilding package executable... Built cli:cli. Hello, Dart!
You've successfully modified and re-run your first Dart program!
Review
#In this lesson, you:
- Verified your Dart SDK installation.
- Used
dart create
to generate a new CLI project. - Ran your Dart program from the terminal using
dart run
. - Identified the
main
function as the program's entry point withinbin/cli.dart
. - Made your first code change and saw the updated output.
Quiz
#Here's a quick quiz to solidify your learning.
Which command is used to create a new Dart project from a template?
- A)
dart new
- B)
dart build
- C)
dart create
- D)
dart init
Next lesson
#In the next lesson, you'll learn how to make your program respond to specific commands by introducing command-line arguments and the const
keyword.
Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-06-13. View source or report an issue.