null is a built-in literal representing the intentional absence of a value. Architecturally, null is the single, immutable instance of the Null class. Under Dart’s sound null safety system, types are non-nullable by default, meaning a variable cannot contain null unless its type explicitly permits it at compile time.
The Null Class and Type Hierarchy
The Null class occupies a specific position within Dart’s unified type system:
Nullis a subtype of all nullable types.- The absolute top type in Dart is
Object?, which is the supertype of all types, includingNull. - The non-nullable top type
Objectdoes not acceptnull. - The bottom type
Nevercannot benull(as it represents a state that never completes).
Nullable Type Syntax
To designate a type as nullable, append a question mark (?) to the type declaration. Conceptually, this creates a union type of the base type and the Null class (e.g., String? acts as String | Null).
Null-Aware Operators
Dart provides specific operators to safely navigate, evaluate, and cast expressions involvingnull at the syntax level:
- Null-assertion operator (
!): Acts as an explicit cast that the static analyzer recognizes, promoting a nullable expression’s static type to its underlying non-nullable type. It throws aTypeErrorat runtime if the operand evaluates tonull.
- Null-coalescing operator (
??): Evaluates to the left operand if it is notnull; otherwise, evaluates to the right operand.
- Null-coalescing assignment operator (
??=): Assigns a value to a variable only if that variable currently evaluates tonull.
- Null-aware access operator (
?.): Conditionally accesses a property or method. If the receiver isnull, the expression short-circuits and evaluates tonullinstead of throwing aNoSuchMethodError.
- Null-aware cascade operator (
?..): Performs a sequence of operations on the same object only if the target object is notnull.
- Null-aware index operator (
?[]): Attempts to access an index of a collection only if the collection itself is notnull.
- Null-aware spread operator (
...?): Conditionally inserts multiple elements into a collection. If the source collection isnull, it is safely ignored rather than throwing an error.
Late Initialization
Thelate modifier alters how the compiler enforces initialization rules. It decouples the declaration of a variable from its initialization, deferring the definite assignment (initialization) check from compile-time to runtime.
When applied to a non-nullable variable, it allows the variable to be declared without an immediate value, provided it is initialized before its first read.
late modifier can also be applied to nullable variables. This enforces that the variable is explicitly initialized before use, allowing developers to distinguish between an uninitialized state (which throws an error if read) and a state explicitly set to null.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More





