Skip to main content
The null assertion operator (!) is a unary postfix operator that converts an expression of a nullable type (T?) to its underlying non-nullable type (T). It explicitly asserts to the compiler that the operand is not null at the specific point of evaluation, overriding static null-safety checks.

Syntax

expression!

Static Analysis Behavior

The operator changes the static type of the expression from T? to T. This allows the resulting value to be assigned to non-nullable targets, passed as non-nullable arguments, or used to access members defined on T. Unlike type promotion via control flow analysis (e.g., if (val != null)), the ! operator enforces the type change unconditionally for that specific expression.

Runtime Behavior

The operator executes a mandatory check on the operand at runtime:
  1. Success: If the operand evaluates to a non-null value, the expression evaluates to that value.
  2. Failure: If the operand evaluates to null, the runtime throws a TypeError (typically with the message “Null check operator used on a null value”). This behavior applies to both the Dart VM (Native) and Dart for the Web.

Code Illustration

void main() {
  int? potentiallyNull = 10;

  // Valid: Changes static type from 'int?' to 'int' for this assignment
  int guaranteedValue = potentiallyNull!;

  potentiallyNull = null;

  // Runtime Error: Throws TypeError
  // Message: "Null check operator used on a null value"
  int crash = potentiallyNull!;
}
Master Dart with Deep Grasping Methodology!Learn More