Skip to main content
The null assertion operator (!) is a postfix unary operator that casts a nullable expression (T?) to its underlying non-nullable type (T). It asserts to both the static type checker and the runtime environment that the operand is not null.

Syntax

expression!

Static Type Mechanics

At compile time, the ! operator performs a hard cast. It instructs the Dart analyzer to treat the expression as known non-nullable, overriding standard control flow analysis. This promotes the static type from nullable to non-nullable, permitting member access or assignment to non-nullable variables.
int? nullableInt = 42;

// Compile-time error: int? cannot be assigned to int
// int nonNullableInt = nullableInt; 

// Valid: The operator casts int? to int
int nonNullableInt = nullableInt!; 

Runtime Mechanics

At runtime, the operator evaluates the operand:
  1. If the operand is not null: The expression evaluates to the value of the operand.
  2. If the operand is null: The operator throws a TypeError, halting execution.
String? valid = "Dart";
String? invalid = null;

print(valid!);   // Output: Dart
print(invalid!); // Unhandled exception: Null check operator used on a null value

Operator Precedence

The null assertion operator is a postfix operator. It shares the highest precedence level with method selectors (.), subscript operators ([]), and function calls (()). It binds more tightly than prefix operators (such as unary negation - or the logical NOT operator !) and arithmetic operators.
class Container {
  int? value;
}

Container? c = Container();

// The ! applies to 'c', allowing access to '.value'
// The result is int? because 'value' is nullable
int? result = c!.value; 
Master Dart with Deep Grasping Methodology!Learn More