@@ -16,42 +16,44 @@ const _desc = r"Don't explicitly initialize variables to null.";
1616const _details = r'''
1717From [Effective Dart](https://dart.dev/guides/language/effective-dart/usage#dont-explicitly-initialize-variables-to-null):
1818
19- **DON'T** explicitly initialize variables to null.
19+ **DON'T** explicitly initialize variables to ` null` .
2020
21- In Dart, a variable or field that is not explicitly initialized automatically
22- gets initialized to null. This is reliably specified by the language. There's
23- no concept of "uninitialized memory" in Dart. Adding `= null` is redundant and
24- unneeded.
21+ If a variable has a non-nullable type or is `final`,
22+ Dart reports a compile error if you try to use it
23+ before it has been definitely initialized.
24+ If the variable is nullable and not `const` or `final`,
25+ then it is implicitly initialized to `null` for you.
26+ There's no concept of "uninitialized memory" in Dart
27+ and no need to explicitly initialize a variable to `null` to be "safe".
28+ Adding `= null` is redundant and unneeded.
2529
2630**BAD:**
2731```dart
28- int _nextId = null;
32+ Item? bestDeal(List<Item> cart) {
33+ Item? bestItem = null;
2934
30- class LazyId {
31- int _id = null;
32-
33- int get id {
34- if (_nextId == null) _nextId = 0;
35- if (_id == null) _id = _nextId++;
36-
37- return _id;
35+ for (final item in cart) {
36+ if (bestItem == null || item.price < bestItem.price) {
37+ bestItem = item;
38+ }
3839 }
40+
41+ return bestItem;
3942}
4043```
4144
4245**GOOD:**
4346```dart
44- int _nextId;
47+ Item? bestDeal(List<Item> cart) {
48+ Item? bestItem;
4549
46- class LazyId {
47- int _id;
48-
49- int get id {
50- if (_nextId == null) _nextId = 0;
51- if (_id == null) _id = _nextId++;
52-
53- return _id;
50+ for (final item in cart) {
51+ if (bestItem == null || item.price < bestItem.price) {
52+ bestItem = item;
53+ }
5454 }
55+
56+ return bestItem;
5557}
5658```
5759
0 commit comments