Dart: Collection if Function

In Dart, the collection if is a smart and simple way to add items to a list, set, or map only when a condition is true. Instead of writing extra if statements outside a collection, Dart lets you place conditions directly inside the collection itself. This makes your code shorter and easier to understand.

Let’s explore how this feature works, using clear examples and simple explanations.

What Is Collection if?

Normally, if you want to add an item to a collection based on a condition, you’d write an if block before adding it. But Dart allows you to skip that and write the condition inside the list, set, or map.

This is especially useful when building dynamic collections, like user menus, form fields, or filtered data lists. It keeps your code clean and avoids unnecessary lines.

Using if Inside Lists

Lists in Dart are ordered collections that can hold multiple items. With collection if, you can include an item only when a certain condition is true.

Here’s a simple example:

void main() {

  bool showBonus = true;

  var messages = [
    'Welcome!',
    if (showBonus) 'You earned a bonus!',
    'Thanks for visiting.',
  ];

  print(messages);

}

In this code, we have a list of messages. The showBonus variable controls whether or not the bonus message is included. If showBonus is true, all three messages appear in the list. If it’s false, the bonus message is skipped, and the list has only two messages.

This helps you build dynamic lists without needing extra logic outside the list.

Using if Inside Sets

Sets in Dart are unordered collections that only contain unique items. You can use collection if in sets the same way you use it in lists.

Here’s an example:

void main() {

  bool isPremium = false;

  var features = {
    'Basic',
    if (isPremium) 'Premium',
  };

  print(features);

}

This set contains a list of features available to a user. If isPremium is true, the set will include both 'Basic' and 'Premium'. If it’s false, it will only include 'Basic'.

Even though sets use curly braces like maps, remember that sets do not use key-value pairs.

Using if Inside Maps

Maps store key-value pairs, like 'name': 'Edward'. Collection if also works inside maps, allowing you to include or skip entire key-value pairs based on a condition.

Here’s an example:

void main() {

  bool applyDiscount = true;

  var item = {
    'name': 'Sneakers',
    'price': 80,
    if (applyDiscount) 'discount': 10,
  };

  print(item);

}

In this example, if applyDiscount is true, the 'discount': 10 pair is added to the map. If the condition is false, that pair is skipped. This makes it easier to build flexible maps without writing extra code outside the map.

Using if-else in Collections

Sometimes you want to include one item if a condition is true, and a different item if it’s false. Dart also allows this using if-else right inside the collection.

With Lists

void main() {

  bool isLoggedIn = false;

  var welcomeMessages = [
    'Hello!',
    if (isLoggedIn) 'Welcome back!' else 'Please log in.',
  ];

  print(welcomeMessages);

}

If isLoggedIn is true, the list will be ['Hello!', 'Welcome back!']
If false, it will be ['Hello!', 'Please log in.']

With Sets

void main() {

  bool hasAccess = false;

  var accessLevels = {
    'Guest',
    if (hasAccess) 'Member' else 'Read-Only',
  };

  print(accessLevels);

}

If hasAccess is true, the set becomes {'Guest', 'Member'}
If false, it becomes {'Guest', 'Read-Only'}

With Maps

void main() {

  bool isStudent = true;

  var userInfo = {
    'name': 'Cherish',
    if (isStudent) 'role': 'Student' else 'role': 'Guest',
  };

  print(userInfo);

}

If isStudent is true, the map becomes:
{'name': 'Cherish', 'role': 'Student'}

If false, it becomes:
{'name': 'Cherish', 'role': 'Guest'}

Table Summary

Collection TypeExample Usage
Listif (condition) 'value' else 'value'
Setif (condition) 'value' else 'value'
Mapif (condition) 'key': 'value' else 'key': 'value'

Real-World Example: Admin Menu

Let’s say you’re creating a menu for an app. You want to show an “Admin Panel” option, but only for users who are admins. With collection if, you can write that easily:

void main() {

  var isAdmin = true;

  var menu = [
    'Home',
    'Profile',
    if (isAdmin) 'Admin Panel',
  ];

  print(menu);

}

If isAdmin is true, the menu includes 'Admin Panel'. If it’s false, that option is automatically left out. This makes your code shorter and avoids writing separate if blocks just to build the menu.

And with else, you can even show a message for non-admins:

void main() {

  var isAdmin = false;

  var menuItems = [
    'Home',
    'Profile',
    if (isAdmin) 'Admin Panel' else 'Request Access',
  ];

  print(menuItems);

}

Why Use Collection if?

Using collection if makes your Dart code cleaner and easier to manage. It’s especially useful when:

  • You need to build lists, sets, or maps based on user roles, settings, or other conditions.
  • You want to avoid writing long if statements just to add or skip a value.
  • You’re working with Flutter UI widgets that depend on changing data.

It’s a small feature, but it can make a big difference in writing readable and efficient Dart code.

Conclusion

The collection if and if-else in Dart are powerful tools for building dynamic lists, sets, and maps. They help you write code that’s short, clean, and easy to follow. Instead of using extra if statements outside the collection, you can now handle conditions right inside it.

Use this feature whenever you want your collections to change based on conditions—whether you’re building a menu, setting up configuration data, or customizing your Flutter UI.