@@ -10,11 +10,11 @@ of variables set to `null`.
10
10
11
11
For example, if a method expects an integer but receives ` null ` ,
12
12
your app causes a runtime error.
13
- This type of error, a null dereference error, can be difficult to debug.
13
+ This error type , a null dereference error, can be difficult to debug.
14
14
15
15
With sound null safety, all variables require a value.
16
16
This means Dart considers all variables _ non-nullable_ .
17
- You can assign values of the declared type only , like ` int i=42 ` .
17
+ You can assign values of the declared type, like ` int i=42 ` .
18
18
You can never assign a value of ` null ` to default variable types.
19
19
To specify that a variable type can have a ` null ` value, add a ` ? ` after
20
20
the type annotation: ` int? i ` .
@@ -42,7 +42,7 @@ final b = Foo();
42
42
```
43
43
44
44
<a id =" creating-variables " ></a >
45
- To indicate that a variable might have the value ` null ` ,
45
+ To show that a variable might have the value ` null ` ,
46
46
just add ` ? ` to its type declaration:
47
47
48
48
``` dart
@@ -54,43 +54,42 @@ int? aNullableInt = null;
54
54
- To learn more about this topic, see
55
55
[ Understanding null safety] ( /null-safety/understanding-null-safety ) .
56
56
57
-
58
57
## Null safety principles
59
58
60
59
Dart supports null safety using the following two core design principles:
61
60
62
- * ** Non-nullable by default** . Unless you explicitly tell Dart that a variable
63
- can be null, it's considered non-nullable. This default was chosen
64
- after research found that non-null was by far the most common choice in APIs.
65
-
66
- * ** Fully sound** . Dart's null safety is sound, which enables compiler optimizations.
67
- If the type system determines that something isn't null, then that thing can _ never_ be
68
- null. Once you migrate your whole project
69
- and its dependencies to null safety,
70
- you reap the full benefits of soundness—not only
71
- fewer bugs, but smaller binaries and faster execution.
61
+ * ** Non-nullable by default** . Unless you set a variable to allow null,
62
+ it's considered non-nullable.
63
+ The Dart team chose this default after research found that APIs
64
+ chose non-null more often.
72
65
66
+ * ** Fully sound** . Dart's null safety is sound,
67
+ which enables compiler optimizations.
68
+ If the type system determines that something isn't null,
69
+ then that thing can _ never_ be null.
70
+ Once you migrate your whole project and its dependencies to null safety,
71
+ you reap the full benefits of soundness—not only fewer bugs,
72
+ but smaller binaries and faster execution.
73
73
74
74
## Dart 3 and null safety
75
75
76
76
Dart 3 has built-in sound null safety.
77
77
Dart 3 prevents code without it from running.
78
78
79
- To learn how to migrate to Dart 3,
80
- check out the [ Dart 3 migration guide] ( /resources/dart-3-migration ) .
79
+ To learn how to migrate to Dart 3,
80
+ review the [ Dart 3 migration guide] ( /resources/dart-3-migration ) .
81
81
Packages developed without null safety support cause issues
82
82
when resolving dependencies:
83
83
84
84
``` terminal
85
85
$ dart pub get
86
86
87
- Because pkg1 doesn't support null safety, version solving failed.
87
+ Because ` pkg1` doesn't support null safety, version solving failed.
88
88
The lower bound of "sdk: '>=2.9.0 <3.0.0'" must be 2.12.0 or higher to enable null safety.
89
89
```
90
90
91
91
Libraries incompatible with Dart 3 cause analysis or compilation errors.
92
92
93
-
94
93
``` terminal
95
94
$ dart analyze .
96
95
Analyzing .... 0.6s
@@ -111,10 +110,10 @@ To resolve these issues:
111
110
112
111
1 . Check for [ null safe versions] ( /null-safety/migration-guide#check-dependency-status )
113
112
of any packages you installed from pub.dev
114
- 2 . [ migrate] ( #migrate ) all of your source code to use sound null safety.
113
+ 2 . [ migrate] ( #migrate ) all your source code to use sound null safety.
115
114
116
- Dart 3 can be found in the stable channels for Dart and Flutter.
117
- To learn more, check out [ the download page] [ ] for details.
115
+ You can find Dart 3 in the stable channels for Dart and Flutter.
116
+ To learn more, review [ the download page] [ ] for details.
118
117
To test your code for Dart 3 compatibility, use Dart 3 or later.
119
118
120
119
``` terminal
@@ -123,7 +122,7 @@ $ dart pub get / flutter pub get # this should resolve without issues
123
122
$ dart analyze / flutter analyze # this should pass without errors
124
123
```
125
124
126
- If the ` pub get ` step fails, check the [ status of the dependencies] [ ] .
125
+ If the ` pub get ` step fails, verify the [ status of the dependencies] [ ] .
127
126
128
127
If the ` analyze ` step fails, update your code to resolve the issues
129
128
listed by the analyzer.
@@ -134,7 +133,7 @@ listed by the analyzer.
134
133
## Dart 2.x and null safety {#enable-null-safety}
135
134
136
135
From Dart 2.12 to 2.19, you need to enable null safety.
137
- You cannot use null safety in SDK versions earlier than Dart 2.12.
136
+ You can't use null safety in SDK versions earlier than Dart 2.12.
138
137
139
138
<a id =" constraints " ></a >
140
139
To enable sound null safety, set the
@@ -170,11 +169,11 @@ $ dart migrate
170
169
` ` `
171
170
172
171
To learn how to migrate your code to null safety,
173
- check out the [migration guide][].
172
+ review the [migration guide][].
174
173
175
174
# # Where to learn more
176
175
177
- To learn more about null safety, check out the following resources :
176
+ To learn more about null safety, review the following resources :
178
177
179
178
* [Null safety codelab][]
180
179
* [Understanding null safety][]
@@ -190,4 +189,3 @@ To learn more about null safety, check out the following resources:
190
189
[#34233]: https://github.com/dart-lang/sdk/issues/34233
191
190
[#49529]: https://github.com/dart-lang/sdk/issues/49529
192
191
[#2357]: https://github.com/dart-lang/language/issues/2357
193
-
0 commit comments