Skip to content

Commit 0f76fe3

Browse files
committed
added ch3
updates to ch3 80 char
1 parent d0374ad commit 0f76fe3

File tree

2 files changed

+459
-0
lines changed

2 files changed

+459
-0
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
---
2+
title: Your first Dart program
3+
description: Create, run, and make your first change to a Dart command-line program.
4+
---
5+
6+
Welcome to Dart. In this chapter, you'll ensure your setup is complete, and then
7+
be guided through creating your first Dart program. This chapter starts simple,
8+
but moves fast!
9+
10+
:::secondary What you'll learn
11+
12+
* Confirm your Dart SDK installation.
13+
* Use `dart create` to generate a new command-line interface (CLI) project.
14+
* Run your Dart program from the terminal using `dart run`.
15+
* Identify the `main` function as the program's entry point.
16+
* Make your first code change and see the updated output.
17+
18+
:::
19+
20+
## Prerequisites
21+
22+
Before you begin this chapter, ensure you have:
23+
24+
* Installed the Dart SDK.
25+
* Reviewed the Dart overview page (if you're new to Dart).
26+
27+
## Task 1: Confirm your Dart setup
28+
29+
First, make sure Dart is ready to go on your system by following these steps.
30+
31+
1. Open your terminal (or command prompt).
32+
33+
2. Run the following command to check your Dart SDK version:
34+
35+
```bash
36+
dart --version
37+
```
38+
39+
3. You should see output similar to this (the version numbers might be
40+
different):
41+
42+
```bash
43+
Dart SDK version: 3.8.0 (stable) (None) on "linux_x64"
44+
```
45+
46+
If you see an error like "command not found," refer to the Dart
47+
installation guide to set up your environment.
48+
49+
## Task 2: Create a new Dart project
50+
51+
Now, create your first Dart command-line application.
52+
53+
1. In your terminal, create a new directory called "dartpedia" to hold your
54+
project. Then switch into that directory.
55+
56+
```bash
57+
mkdir dartpedia
58+
cd dartpedia
59+
```
60+
61+
2. Run the following command:
62+
63+
```bash
64+
dart create cli
65+
```
66+
67+
This command uses the `dart create` command to generate a basic Dart project
68+
named "cli" (for Command Line Interface). It sets up the essential files and
69+
directories you need.
70+
71+
3. You should see output similar to this, confirming the project creation:
72+
73+
```bash
74+
Creating cli using template console...
75+
76+
.gitignore
77+
analysis_options.yaml
78+
CHANGELOG.md
79+
pubspec.yaml
80+
README.md
81+
bin/cli.dart
82+
lib/cli.dart
83+
test/cli_test.dart
84+
85+
Running pub get... 1.2s
86+
Resolving dependencies...
87+
Downloading packages...
88+
Changed 49 dependencies!
89+
1 package has newer versions incompatible with dependency constraints.
90+
Try `dart pub outdated` for more information.
91+
92+
Created project cli in cli! In order to get started, run the following commands:
93+
94+
cd cli
95+
dart run
96+
```
97+
98+
:::note
99+
The `dart create` command created a number of files. Don't worry about these
100+
now. Their specifics will be covered in future chapters.
101+
:::
102+
103+
## Task 3: Run your first Dart program
104+
105+
1. Navigate into your new project directory:
106+
107+
```bash
108+
cd cli
109+
```
110+
111+
2. Run the default application:
112+
113+
```bash
114+
dart run
115+
```
116+
117+
This command tells Dart to execute your program.
118+
119+
3. You should see the following output:
120+
121+
```bash
122+
Building package executable...
123+
Built cli:cli.
124+
Hello world: 42!
125+
```
126+
127+
Congratulations! You've successfully run your first Dart program!
128+
129+
## Task 4: Make your first code change
130+
131+
Next, modify the code that generated `Hello world: 42!`.
132+
133+
1. Open the `bin/cli.dart` file in your editor.
134+
135+
The `bin/` directory is where your executable code lives. `cli.dart` is the
136+
entry point of your application.
137+
138+
Inside, you'll see the `main` function. Every Dart program starts executing
139+
from its `main` function.
140+
141+
2. Your `bin/cli.dart` should look like this:
142+
143+
```dart
144+
import 'package:cli/cli.dart' as cli;
145+
146+
void main(List<String> arguments) {
147+
print('Hello world: ${cli.calculate()}!');
148+
}
149+
```
150+
151+
3. Simplify the output for now. Comment out the first line (you don't need
152+
this import statement), and change the `print` statement to display a simple
153+
greeting:
154+
155+
```dart
156+
// import 'package:cli/cli.dart' as cli;
157+
158+
void main(List<String> arguments) {
159+
print('Hello, Dart!'); // Change this line
160+
}
161+
```
162+
163+
:::note
164+
For now, comment out the `import 'package:cli/cli.dart'` line to avoid
165+
an unused import warning. You will learn about this and fully remove the
166+
line later.
167+
:::
168+
169+
4. Save your file and run your program again:
170+
171+
```bash
172+
dart run
173+
```
174+
175+
5. You should now see:
176+
177+
```bash
178+
Building package executable...
179+
Built cli:cli.
180+
Hello, Dart!
181+
```
182+
183+
You've successfully modified and re-run your first Dart program!
184+
185+
## Review
186+
187+
In this lesson, you:
188+
189+
* Confirmed your Dart SDK installation.
190+
* Used `dart create` to generate a new CLI project.
191+
* Ran your Dart program from the terminal using `dart run`.
192+
* Identified the `main` function as the program's entry point within
193+
`bin/cli.dart`.
194+
* Made your first code change and saw the updated output.
195+
196+
## Quiz
197+
198+
Here's a quick quiz to solidify your learning.
199+
200+
:::note
201+
You'll see these quizzes throughout this tutorial. Feel free to skip them if you
202+
want.
203+
:::
204+
205+
Which command is used to create a new Dart project from a template?
206+
207+
* A) `dart new`
208+
* B) `dart build`
209+
* C) `dart create`
210+
* D) `dart init`
211+
212+
## Next lesson
213+
214+
In the next lesson, you'll learn how to make your program respond to specific
215+
commands by introducing command-line arguments and the `const` keyword.

0 commit comments

Comments
 (0)