| OLD | NEW |
| 1 Here's an example of creating a variable and assigning a value to it: | 1 Here's an example of creating a variable and assigning a value to it: |
| 2 | 2 |
| 3 {% highlight dart %} | 3 {% highlight dart %} |
| 4 var name = 'Bob'; | 4 var name = 'Bob'; |
| 5 {% endhighlight %} | 5 {% endhighlight %} |
| 6 | 6 |
| 7 Variables are references. The variable named `name` contains a reference | 7 Variables are references. The variable named `name` contains a reference |
| 8 to a String object with a value of "Bob". | 8 to a String object with a value of "Bob". |
| 9 | 9 |
| 10 #### Default value | 10 #### Default value |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 You have the option of adding static types to your variable declarations: | 22 You have the option of adding static types to your variable declarations: |
| 23 | 23 |
| 24 {% highlight dart %} | 24 {% highlight dart %} |
| 25 String name = 'Bob'; | 25 String name = 'Bob'; |
| 26 {% endhighlight %} | 26 {% endhighlight %} |
| 27 | 27 |
| 28 Adding types is a way to clearly express your intent. | 28 Adding types is a way to clearly express your intent. |
| 29 Tools like compilers and editors can use these types to | 29 Tools like compilers and editors can use these types to |
| 30 help you, by providing early warnings for bugs and code completion. | 30 help you, by providing early warnings for bugs and code completion. |
| 31 | 31 |
| 32 <aside class="note" markdown="1"> | 32 <aside markdown="1"> |
| 33 **Note:** This chapter follows the | 33 <div class="alert alert-info"> |
| 34 [style guide recommendation](/articles/style-guide/#type-annotations) | 34 <strong>Note:</strong> |
| 35 of using **var**, rather than type annotations, for local variables. | 35 This chapter follows the |
| 36 <a href="/articles/style-guide/#type-annotations">style guide recommendation
</a> |
| 37 of using <strong>var</strong>, rather than type annotations, for local varia
bles. |
| 38 </div> |
| 36 </aside> | 39 </aside> |
| 37 | 40 |
| 38 #### final | 41 #### final |
| 39 | 42 |
| 40 If you never intend to change a variable, use `final` instead of var or in | 43 If you never intend to change a variable, use `final` instead of var or in |
| 41 addition to a type. Once a `final` variable is set, it can't be changed. | 44 addition to a type. Once a `final` variable is set, it can't be changed. |
| 42 | 45 |
| 43 {% highlight dart %} | 46 {% highlight dart %} |
| 44 final name = 'Bob'; // Or: final String name = 'Bob'; | 47 final name = 'Bob'; // Or: final String name = 'Bob'; |
| 45 name = 'Alice'; // ERROR | 48 name = 'Alice'; // ERROR |
| 46 {% endhighlight %} | 49 {% endhighlight %} |
| 47 | 50 |
| 48 #### Summary | 51 #### Summary |
| 49 | 52 |
| 50 Although Dart variables are optionally typed, | 53 Although Dart variables are optionally typed, |
| 51 we generally recommend using types | 54 we generally recommend using types |
| 52 because they make your intent clear. | 55 because they make your intent clear. |
| 53 Variables can be marked as `final`, locking the value. Uninitialized variables | 56 Variables can be marked as `final`, locking the value. Uninitialized variables |
| 54 have an initial value of `null`. | 57 have an initial value of `null`. |
| OLD | NEW |