Ternary Operator in Flutter

Ternary Operator in Flutter

I want to show and hide HomeCategory objects based on a bool. I have this in place currently:

  _isOn ? HomeCategory(0, Icons.check, Colors.blue[800], "Check In", [Task(0, "Check In", true),]) : "",

However it throws an error because I am passing an empty string if its turned off. How can I pass it an empty object or hide it? It is a list like so:

   _isOn ? HomeCategory(0, Icons.check, Colors.blue[800], "Check In", [Task(0, "Check In", true),]) : "",
    HomeCategory(1, Icons.chat_bubble, Colors.red, "Forums", [ Task(1, "Questions", true),]),
    HomeCategory(2, Icons.star, Colors.blue[200], "Goals", [ Task(2, "Goals", true),]),
    HomeCategory(3, Icons.monetization_on, Colors.orange[700], "Budget", [ Task(3, "Budget", true),]),
    HomeCategory(4, Icons.shopping_basket, Colors.brown[300], "Shopping", [ Task(4, "Items", true),]),
    HomeCategory(5, Icons.calendar_today, Colors.purple[900], "My Day & Calendar", [ Task(4, "Events", false),]),
    HomeCategory(6, Icons.check_circle_outline, Colors.teal[700], "Check Out", [ Task(4, "Tasks", false),]),
    HomeCategory(7, Icons.dock, Colors.grey[700], "Connect", [ Task(4, "Tasks", false),]),
    HomeCategory(8, Icons.local_pharmacy, Colors.green, "My Medication", [ Task(4, "Tasks", false),]),
    HomeCategory(9, Icons.settings, Colors.yellow[800], "Settings", [ Task(4, "Tasks", false),]),

4 Answers

You can do this :

_isOn ? HomeCategory() : SizedBox()

But since it is in a List<Widget>, an if condition is cleaner :

if(_isOn)
    HomeCategory()

You don't need to use ternary operators at all. Ever since Dart 2.3, you can use control statements in collections (i.e. lists and maps) to add things conditionally or in a loop. In your case, you would want to use the conditional syntax:

Column( // as an example
  children: [
    if (isOn0) HomeCategory(0, Icons.check, Colors.blue[800], "Check In", [Task(0, "Check In", true),]),
    if (isOn1) HomeCategory(1, Icons.chat_bubble, Colors.red, "Forums", [ Task(1, "Questions", true),]),
    if (isOn2) HomeCategory(2, Icons.star, Colors.blue[200], "Goals", [ Task(2, "Goals", true),]),
    ...
  ],
),

You could make this even cleaner by combining it with the loop syntax:

Column(
  children: [
    for (var category in categories)
      if (category.isOn)
        HomeCategory(
          category.id, 
          category.icon, 
          category.color, 
          category.message, 
          category.tasks,
        ),
  ],
),

EDIT: If you absolutely must have an explicit incrementing index for each HomeCategory widget, you can do it like this:

Conditional-Only Syntax:

int categoryIdx = 0; // declare at the top of the build method
...
Column( // as an example
  children: [
    if (isOn0) HomeCategory(categoryIdx++, Icons.check, Colors.blue[800], "Check In", [Task(0, "Check In", true),]),
    if (isOn1) HomeCategory(categoryIdx++, Icons.chat_bubble, Colors.red, "Forums", [ Task(1, "Questions", true),]),
    if (isOn2) HomeCategory(categoryIdx++, Icons.star, Colors.blue[200], "Goals", [ Task(2, "Goals", true),]),
    ...
  ],
),

Conditional + Loop Syntax:

int categoryIdx = 0; // declare at the top of the build method
...
Column(
  children: [
    for (var category in categories)
      if (category.isOn)
        HomeCategory(
          categoryIdx++, 
          category.icon, 
          category.color, 
          category.message, 
          category.tasks,
        ),
  ],
),
4

I am utils so:

children: [
        if (_selectedIndex == 0) ...[
          DayScreen(),
        ] else if(_selectedIndex == 1)...[
          StatsScreen(),
        ],
    ],

If you want to use ternary operator to handle widgets, then in else you can put Container(), or Sizedbox() widget.

 condition ? HomeCategory("Check In") : Container()

I use container or sizedbox widget everytime, when I dont have anything for else condition, so I pass Container(), as it doesn't occupy any space, or render any style

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.