null and subsequently matches an inner pattern against that non-nullable value. Syntactically represented by a trailing question mark (?), it allows for safe extraction and type promotion within pattern matching contexts, such as switch statements, switch expressions, and if-case constructs.
Syntax
The null-check pattern consists of a subpattern followed immediately by?.
Semantics and Execution
When the Dart runtime evaluates a null-check pattern against a subject value (the scrutinee):- Null Validation: The runtime checks if the subject value is
null. - Match Failure: If the subject is
null, the pattern fails to match. Execution continues to the nextcaseor branch. It does not throw an exception. - Type Promotion: If the subject is not
null, the value is treated as its non-nullable counterpart. - Subpattern Evaluation: The
subpatternis then matched against the promoted, non-nullable value.
Variable Binding
The null-check pattern is frequently combined with a variable declaration pattern to bind the non-nullable value to a new variable.Recursive Composition
The null-check pattern can be nested within other patterns or contain other patterns. This allows for deep validation of nullable structures.Distinction from Null-Assert Pattern
The null-check pattern (?) must be distinguished from the null-assert pattern (!).
- Null-Check (
?): If the value isnull, the match fails safely. Control flow moves to the next case. - Null-Assert (
!): If the value isnull, the runtime throws aTypeError.
Master Dart with Deep Grasping Methodology!Learn More





