Skip to content

Files

Latest commit

 

History

History
47 lines (34 loc) · 935 Bytes

avoid_init_to_null.md

File metadata and controls

47 lines (34 loc) · 935 Bytes

Pattern: Use of initialize to null

Issue: -

Description

In Dart, a variable or field that is not explicitly initialized automatically gets initialized to null. This is reliably specified by the language. There's no concept of "uninitialized memory" in Dart. Adding = null is redundant and unneeded.

Example of correct code:

int _nextId;

class LazyId {
 int _id;

 int get id {
  if (_nextId == null) _nextId = 0;
  if (_id == null) _id = _nextId++;

  return _id;
 }
}

Example of incorrect code:

int _nextId = null;

class LazyId {
 int _id = null;

 int get id {
  if (_nextId == null) _nextId = 0;
  if (_id == null) _id = _nextId++;

  return _id;
 }
}

Further Reading