Spread Operator and Collection If in Dart#
Using the Spread Operator (...
)#
How It Works#
The spread operator (...
) in Dart is used to insert multiple elements from another collection into a list or set. It allows you to “spread” the elements of an existing collection into a new collection. The null-aware spread operator (...?
) can be used when you want to include elements from a collection only if it is non-null, preventing null pointer exceptions.
- The spread operator
...
takes the elements of one collection and expands them into another collection. - The null-aware spread operator
...?
ensures that if the collection is null, it won’t throw an error and will be skipped.
How to Use#
You can use the spread operator when you need to concatenate or combine multiple collections into one. Similarly, the null-aware spread operator helps safely handle collections that may be null.
Example: Using the Spread Operator#
void main() {
List<int> firstList = [1, 2, 3];
List<int> secondList = [4, 5, 6];
// Using the spread operator to combine two lists
List<int> combinedList = [...firstList, ...secondList];
print(combinedList); // Output: [1, 2, 3, 4, 5, 6]
}
Example: Using the Null-Aware Spread Operator#
void main() {
List<int>? nullableList = null;
List<int> numbers = [1, 2, 3, ...?nullableList];
print(numbers); // Output: [1, 2, 3]
}
- In this example, the null-aware spread operator prevents a null list from being included in the numbers list.
Conditional Elements in Collections#
- How It Works
Dart allows you to add elements to a collection based on a condition using collection if. This feature is useful for dynamically including elements in a list or set depending on certain conditions.
- You can use if statements inside a collection literal to conditionally include elements.
How to Use#
Use the if keyword inside a collection literal to add elements conditionally. This can be especially helpful in cases where you want to add elements only if certain conditions are met.
Example: Using Collection If#
void main() {
bool addMoreNumbers = true;
List<int> numbers = [
1, 2, 3,
if (addMoreNumbers) 4, 5, 6
];
print(numbers); // Output: [1, 2, 3, 4, 5, 6]
}
- In this example, elements 4, 5, and 6 are added to the numbers list only if addMoreNumbers is true.
Example: Combining Spread Operator and Collection If#
void main() {
List<int> firstList = [1, 2, 3];
bool includeSecondList = false;
List<int> secondList = [4, 5, 6];
// Combine lists and use collection if to conditionally include secondList
List<int> numbers = [
...firstList,
if (includeSecondList) ...secondList
];
print(numbers); // Output: [1, 2, 3]
}
- In this example, elements 4, 5, and 6 are added to the numbers list only if addMoreNumbers is true.
##Example: Combining Spread Operator and Collection If
void main() {
List<int> firstList = [1, 2, 3];
bool includeSecondList = false;
List<int> secondList = [4, 5, 6];
// Combine lists and use collection if to conditionally include secondList
List<int> numbers = [
...firstList,
if (includeSecondList) ...secondList
];
print(numbers); // Output: [1, 2, 3]
}
- Here, the second list is only included if includeSecondList is true.
Conclusion#
The spread operator (…) and collection if provide powerful ways to work with collections in Dart. They allow you to combine collections and add elements conditionally, all while keeping your code concise and readable. These features make Dart collections highly flexible and easy to manipulate.