You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/get-started/add-commands.md
+19-21Lines changed: 19 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -68,6 +68,7 @@ explore the Dart syntax for it.
68
68
}
69
69
}
70
70
```
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.
71
72
72
73
1. **Test the `version` command:** Run your application with the version
73
74
argument:
@@ -120,7 +121,7 @@ explore the Dart syntax for it.
120
121
you've implemented control flow in the `main` function, review the
121
122
code that was added for it.
122
123
123
-
* `arguments.isNotEmpty` checks if any command-line arguments were
124
+
* `arguments.isEmpty` checks if no command-line arguments were
124
125
provided.
125
126
* `arguments.first` accesses the very first argument, which you're using as
126
127
our command.
@@ -212,7 +213,7 @@ null checks, and string interpolation.
212
213
be null, even in production. The purpose of null-safety isn't
213
214
to stop you from ever using null in your code, because
214
215
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
216
217
careful about it. Along with the analyzer, this helps prevent
217
218
one of the most common runtime crashes in programming:
218
219
null-pointer errors.
@@ -243,6 +244,7 @@ null checks, and string interpolation.
243
244
244
245
Highlights from the preceding code:
245
246
247
+
* `final` variables can only be set once and are used when you never intend to change the variable again in the code.
246
248
* `arguments.sublist(1)` creates a new list
247
249
containing all elements of the `arguments` list *after* the first
248
250
element (which was `search`).
@@ -294,12 +296,13 @@ null checks, and string interpolation.
294
296
295
297
```dart
296
298
void searchWikipedia(List<String>? arguments) {
297
-
late String articleTitle;
299
+
final String articleTitle;
298
300
299
301
// If the user didn't pass in arguments, request an article title.
300
302
if (arguments == null || arguments.isEmpty) {
301
303
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() ?? '';
303
306
} else {
304
307
// Otherwise, join the arguments into the CLI into a single string
305
308
articleTitle = arguments.join(' ');
@@ -312,29 +315,22 @@ null checks, and string interpolation.
312
315
This preceding code block introduces a few
313
316
key concepts:
314
317
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.
320
319
* An `if/else` statement then checks if command-line arguments for the
321
320
search were provided.
322
321
* 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.
324
323
* If arguments *are* present, it uses `arguments.join(' ')` to combine
325
324
them into a single search string.
326
325
327
326
Highlights from the preceding code:
328
327
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.
334
329
* `arguments.join(' ')`: concatenates all elements of the `arguments` list
335
330
into a single string, using a space as the separator. For example,
336
331
`['Dart', 'Programming']` becomes `"Dart Programming"`. This is crucial
337
332
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.
338
334
339
335
1. **Finish `searchWikipedia` to print mock search results:** Update `searchWikipedia` to display
340
336
messages that look like our program found something. This helps us see what
@@ -346,13 +342,15 @@ null checks, and string interpolation.
346
342
347
343
```dart
348
344
void searchWikipedia(List<String>? arguments) {
349
-
late String? articleTitle;
345
+
final String articleTitle;
350
346
347
+
// If the user didn't pass in arguments, request an article title.
351
348
if (arguments == null || arguments.isEmpty) {
352
349
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() ?? '';
355
352
} else {
353
+
// Otherwise, join the arguments into the CLI into a single string
356
354
articleTitle = arguments.join(' ');
357
355
}
358
356
@@ -400,10 +398,10 @@ In this chapter, you learned:
400
398
401
399
* **Control flow:** Using `if/else` statements to control the execution flow
402
400
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`,
405
403
`.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.
407
405
* **Functions:** Defining and calling functions.
408
406
* **String interpolation:** Embedding variables in strings using `$`.
409
407
* **Input/Output:** Reading user input from the console using `stdin.readLineSync()`.
0 commit comments