Skip to content

Commit 7da3116

Browse files
authored
Fix for Misspelling and Bug in Code Sample (#6803)
Fixed a spelling error and a bug in the code. Fixes https://dart.dev/get-started/add-commands --- - [x] I’ve reviewed the contributor guide and applied the relevant portions to this PR. - [x] This PR doesn't contain automatically generated corrections or text (Grammarly, LLMs, and similar). - [x] This PR follows the [Google Developer Documentation Style Guidelines](https://developers.google.com/style) — for example, it doesn't use _i.e._ or _e.g._, and it avoids _I_ and _we_ (first person). - [x] This PR uses [semantic line breaks](https://github.com/dart-lang/site-shared/blob/main/doc/writing-for-dart-and-flutter-websites.md#semantic-line-breaks) of 80 characters or fewer.
1 parent e7bcd8c commit 7da3116

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

src/content/get-started/add-commands.md

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ explore the Dart syntax for it.
6868
}
6969
}
7070
```
71+
The `$version` syntax is called string interpolation. It lets you embed the value of the variable directly into a string by prefixing the variable name with a `$` sign.
7172
7273
1. **Test the `version` command:** Run your application with the version
7374
argument:
@@ -120,7 +121,7 @@ explore the Dart syntax for it.
120121
you've implemented control flow in the `main` function, review the
121122
code that was added for it.
122123
123-
* `arguments.isNotEmpty` checks if any command-line arguments were
124+
* `arguments.isEmpty` checks if no command-line arguments were
124125
provided.
125126
* `arguments.first` accesses the very first argument, which you're using as
126127
our command.
@@ -212,7 +213,7 @@ null checks, and string interpolation.
212213
be null, even in production. The purpose of null-safety isn't
213214
to stop you from ever using null in your code, because
214215
representing the absense of a value can be valuable. Rather,
215-
it's to force you to consider nullability and therefor be more
216+
it's to force you to consider nullability and therefore be more
216217
careful about it. Along with the analyzer, this helps prevent
217218
one of the most common runtime crashes in programming:
218219
null-pointer errors.
@@ -243,6 +244,7 @@ null checks, and string interpolation.
243244
244245
Highlights from the preceding code:
245246
247+
* `final` variables can only be set once and are used when you never intend to change the variable again in the code.
246248
* `arguments.sublist(1)` creates a new list
247249
containing all elements of the `arguments` list *after* the first
248250
element (which was `search`).
@@ -294,12 +296,13 @@ null checks, and string interpolation.
294296
295297
```dart
296298
void searchWikipedia(List<String>? arguments) {
297-
late String articleTitle;
299+
final String articleTitle;
298300
299301
// If the user didn't pass in arguments, request an article title.
300302
if (arguments == null || arguments.isEmpty) {
301303
print('Please provide an article title.');
302-
articleTitle = stdin.readLineSync(); // Await input from the user
304+
// Await input and provide a default empty string if the input is null.
305+
articleTitle = stdin.readLineSync() ?? '';
303306
} else {
304307
// Otherwise, join the arguments into the CLI into a single string
305308
articleTitle = arguments.join(' ');
@@ -312,29 +315,22 @@ null checks, and string interpolation.
312315
This preceding code block introduces a few
313316
key concepts:
314317
315-
* It declares a `late String? articleTitle` variable which will
316-
hold the full search query, whether it comes from the command
317-
line or user input. `late` is a keyword that tells the
318-
analyzer that you, the programmer, promise that this variable
319-
won't be null by the time it's used.
318+
* It declares a `final String articleTitle` variable. This allows static analysis to detect that `articleTitle` will be a `String` and won't be null.
320319
* An `if/else` statement then checks if command-line arguments for the
321320
search were provided.
322321
* If arguments are missing, it prompts the user, reads input using
323-
`stdin.readLineSync()`, and performs null and empty checks.
322+
`stdin.readLineSync()`, and safely handles cases where no input is given.
324323
* If arguments *are* present, it uses `arguments.join(' ')` to combine
325324
them into a single search string.
326325
327326
Highlights from the preceding code:
328327
329-
* `stdin.readLineSync()` reads a line of text typed by the user into the
330-
console. Its return type is `String?`.
331-
* `if (inputFromStdin == null || inputFromStdin.isEmpty)` performs a
332-
null check and an empty string check. If either is true, the program
333-
prints a message and `return`s, exiting the function.
328+
* `stdin.readLineSync() ?? ''` reads the input from the user. While `stdin.readLineSync()` can return null, the null-coalescing operator (`??`) provides a default empty string (`''`). This is a concise way to ensure that the variable is a non-null String.
334329
* `arguments.join(' ')`: concatenates all elements of the `arguments` list
335330
into a single string, using a space as the separator. For example,
336331
`['Dart', 'Programming']` becomes `"Dart Programming"`. This is crucial
337332
for treating multi-word command-line inputs as a single search phrase.
333+
* Dart static analysis can detect that `articleTitle` is guaranteed to be initialized when the print statement is executed. No matter which path is taken through this function body, the variable is non-nullable.
338334
339335
1. **Finish `searchWikipedia` to print mock search results:** Update `searchWikipedia` to display
340336
messages that look like our program found something. This helps us see what
@@ -346,13 +342,15 @@ null checks, and string interpolation.
346342
347343
```dart
348344
void searchWikipedia(List<String>? arguments) {
349-
late String? articleTitle;
345+
final String articleTitle;
350346
347+
// If the user didn't pass in arguments, request an article title.
351348
if (arguments == null || arguments.isEmpty) {
352349
print('Please provide an article title.');
353-
articleTitle = stdin.readLineSync();
354-
return; // Exits here if input is from stdin
350+
// Await input and provide a default empty string if the input is null.
351+
articleTitle = stdin.readLineSync() ?? '';
355352
} else {
353+
// Otherwise, join the arguments into the CLI into a single string
356354
articleTitle = arguments.join(' ');
357355
}
358356
@@ -400,10 +398,10 @@ In this chapter, you learned:
400398
401399
* **Control flow:** Using `if/else` statements to control the execution flow
402400
of your program.
403-
* **Variables and Constants:** Declaring variables with `var`, `const`, and `late String?`.
404-
* **Lists:** Creating and manipulating lists using `.isNotEmpty`, `.first`,
401+
* **Variables and Constants:** Declaring variables with `var`, `const`, and `final String`.
402+
* **Lists:** Creating and manipulating lists using `.isEmpty`, `.first`,
405403
`.sublist`, and `.join()`.
406-
* **Null Safety:** Understanding nullability (`?`) and using null checks.
404+
* **Null Safety:** Understanding nullability (`?`) and using null checks. Handling potential null values with the null-coalescing operator (`??`) to provide default values.
407405
* **Functions:** Defining and calling functions.
408406
* **String interpolation:** Embedding variables in strings using `$`.
409407
* **Input/Output:** Reading user input from the console using `stdin.readLineSync()`.

0 commit comments

Comments
 (0)