Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ternary operator in flutter

Tags:

flutter

dart

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),]),
like image 504
Sam Cromer Avatar asked Jan 11 '20 01:01

Sam Cromer


People also ask

What is ternary operator in Dart?

Overview. The conditional ternary operator assigns a value to a variable based on some conditions. It is used in place of the if statement. This operator also controls the flow of logical execution in the code. Note: The ternary operator is the only dart operator that takes 3 operators.

What is ternary operator in Verilog?

An operator that selects between two expressions within an AHDL or Verilog HDL arithmetic expression.

Which is an example of a ternary operator?

Example: C Ternary Operatorprintf("You can vote") - expression1 that is executed if condition is true. printf("You cannot vote") - expression2 that is executed if condition is false.

Can ternary operator have 3 conditions?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.


1 Answers

You can do this :

_isOn ? HomeCategory() : SizedBox()

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

if(_isOn)
    HomeCategory()
like image 71
MickaelHrndz Avatar answered Sep 23 '22 06:09

MickaelHrndz