CollectionDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
if is a declarative control flow element used directly within Dart collection literals (List, Set, and Map) to conditionally insert elements during instantiation. It evaluates a boolean expression and includes the subsequent element, sequence, or spread only if the expression resolves to true.
Unlike standard if statements, which control program execution flow, collection if operates as an element generator within the lexical scope of a collection literal.
Syntax and Mechanics
The operator supports standardif, if-else, and Dart 3.0+ if-case pattern matching.
Standard if and if-else
Set and Map literals. For maps, the conditional element must be a valid key-value pair.
if-case allows conditional element inclusion based on whether a value matches a specific pattern, simultaneously extracting destructured variables.
Technical Characteristics
Omission vs. Null Insertion When a collectionif condition evaluates to false and no else branch is provided, the element is entirely omitted from the collection. It does not insert a null value, and the collection’s length property is not incremented.
Compile-Time Constant Evaluation
Collection if is fully supported within const collection literals, provided that the condition itself is a compile-time constant expression. The Dart compiler evaluates the condition during compilation, resulting in a fixed, immutable collection without runtime overhead.
if participate in the compiler’s type inference. If the collection’s generic type is not explicitly declared, the Dart analyzer calculates the Least Upper Bound (LUB) of all elements, including those in both branches of an if-else construct.
if can be composed with the spread operator (...) to conditionally insert multiple elements from an iterable.
if elements are evaluated synchronously at runtime during the instantiation of the collection literal. They are not lazy, nor can they be used to mutate a collection after it has been allocated in memory.
Master Dart with Deep Grasping Methodology!Learn More





