The null-assert pattern (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
!) is a unary pattern that matches a value, asserts that the value is not null, and casts it to its underlying non-null type. If the matched value evaluates to null, the pattern immediately throws a runtime exception.
Syntax
! operator. It binds the non-null value to the subpattern.
Evaluation Semantics
When the Dart runtime evaluates a null-assert pattern against a valuev:
- It checks if
vis null. - If
vis null, execution halts, and aTypeErroris thrown. - If
vis not null,vis cast to its non-null base type and passed to the inner<subpattern>for further matching or binding.
Type Promotion and Binding
The primary mechanical function of the null-assert pattern is to force type promotion during destructuring or pattern matching.Destructuring with Null-Assert
The pattern can be nested within record, list, object, or map patterns to assert the non-nullability of specific elements during extraction.Contrast with Null-Check Pattern
Mechanically, the null-assert pattern (!) differs strictly from the null-check pattern (?). While ? safely fails the match on a null value and allows the switch or if-case to evaluate the next branch, ! acts as a hard assertion, terminating the control flow with an exception if a null value is encountered.
Master Dart with Deep Grasping Methodology!Learn More





