Composing Dart Collections with Spread, Collection-If, and Collection-For
Master declarative UI and data composition in Dart 3 using spread operators (... and ...?), collection-if, and collection-for across lists, sets, and maps.
Published on • December 5, 2026
AI Assistant

Before Dart 2.3, building dynamic lists in Flutter widget trees required verbose imperative code: instantiating a mutable list, appending conditional items using if statements, and merging other lists via .addAll().
Dart introduced three declarative collection operators that transformed how developers write Flutter UIs and construct datasets:
- Spread Operators (
...and...?) - Collection-If (
if (condition) element) - Collection-For (
for (var item in items) element)
In this guide, we dive into the mechanics, edge cases, and best practices of composing collections declaratively.
The Spread Operator (... and ...?)
The spread operator inserts all elements of an iterable directly into another collection literal.
void main() {
final baseFeatures = ['Authentication', 'Database'];
final proFeatures = ['Analytics', 'Realtime Sync'];
// Flattens both lists into a single new list
final enterpriseFeatures = [
'Billing',
...baseFeatures,
...proFeatures,
'Custom SLA',
];
print(enterpriseFeatures);
// [Billing, Authentication, Database, Analytics, Realtime Sync, Custom SLA]
}
The Null-Aware Spread Operator (...?)
If the list being spread might be null, use ...? to gracefully omit it without throwing a NullCheckError:
List<String>? fetchBetaFlags() => null; // May return null
void main() {
final activeFlags = [
'DarkMode',
...?fetchBetaFlags(), // Safe: does nothing if null
'PushNotifications',
];
print(activeFlags); // [DarkMode, PushNotifications]
}
Collection-If: Declarative Conditional Elements
Collection-if allows inserting elements into a collection only when a boolean condition evaluates to true. It supports both single if and if-else branches:
List<Widget> buildNavigationItems({required bool isAdmin, required bool isPremium}) {
return [
const Text('Home'),
const Text('Profile'),
if (isAdmin) const Text('Admin Dashboard'),
if (isPremium)
const Text('VIP Support')
else
const Text('Upgrade to VIP'),
];
}
Collection-If with Pattern Matching (Dart 3)
In modern Dart 3, collection-if supports case pattern matching and variable binding directly inside the collection literal:
sealed class UserState {}
class LoggedIn extends UserState { final String username; LoggedIn(this.username); }
class Guest extends UserState {}
List<String> getMenuItems(UserState state) {
return [
'Terms of Service',
'Help Center',
if (state case LoggedIn(:final username)) 'Account ($username)',
];
}
Collection-For: Transforming and Mapping Data
Collection-for iterates over an iterable, emitting one or more elements into the surrounding collection:
void main() {
final rawNumbers = [1, 2, 3, 4, 5];
// Creates formatted string tags
final tagList = [
'Header',
for (final n in rawNumbers) 'Tag #$n',
'Footer',
];
print(tagList);
// [Header, Tag #1, Tag #2, Tag #3, Tag #4, Tag #5, Footer]
}
Composing Sets and Maps
Spread, collection-if, and collection-for are not restricted to List. They work equally well with Set and Map:
Composing Maps
Map<String, dynamic> buildUserPayload({required String name, String? email, bool includeMetadata = false}) {
return {
'name': name,
if (email != null) 'email': email,
if (includeMetadata) ...{
'created_at': DateTime.now().toIso8601String(),
'client': 'mobile_app',
},
};
}
Composing Sets
void main() {
final primaryColors = {'Red', 'Green', 'Blue'};
final extraColors = {'Yellow', 'Blue'}; // Notice 'Blue' duplicate
final allColors = {
'Black',
...primaryColors,
...extraColors,
};
print(allColors); // {Black, Red, Green, Blue, Yellow} - Sets automatically deduplicate!
}
Combining All Three in Flutter Widgets
The real power of collection operators shines in Flutter widget trees, allowing clean, single-expression builds without nested helper functions:
Widget buildWidgetList(List<Item> items, bool hasMore, bool isLoading) {
return ListView(
children: [
const HeaderBanner(),
for (final item in items)
if (item.isVisible)
ItemCard(item: item),
if (isLoading)
const LoadingIndicator()
else if (hasMore)
const LoadMoreButton(),
],
);
}
Key Takeaways
- Use
...to unpack lists, and...?when the source iterable may benull. - Replace imperative
.add()chains withif (condition)inside collection literals. - Use
for (var item in list)to dynamically generate children in Flutter widget arrays. - Take advantage of Dart 3
if (value case Pattern)to guard and unwrap data in a single step.