Is there any ternary operator for just single if condition in dart?

607

No.

The conditional operator ?/: in Dart requires all three operands. It does so because all expressions must have a value, and if you could do just e1 ? e2, then the expression has no value if e1 is false.

It's not impossible to conceive of a binary conditional operator where the missing expression defaults to null, say (e1?:elseExpression) or (e1?thenExpression:), but then you can also just write the null, and saving four letters is probably not worth the potential loss of readability.

Ob-nitpick. The conditional operator in Dart is one of two ternary operators (operators requiring three operands, like binary operators require two operands), the other ternary operator being []=.

Share:
607
Devarsh Ranpara
Author by

Devarsh Ranpara

Flutter Developer at Simform Solutions, Exploring IoT, Blockchain.

Updated on December 17, 2022

Comments

  • Devarsh Ranpara
    Devarsh Ranpara over 1 year

    In dart we have plenty of ternary operators. But do we have one ternary operator just for if condition?

    Example

    In condition

    if (num == 1){
      print(true);
    } else {
      print(false);
    }
    

    In ternary

    print(num == 1 ? true : false);
    

    So do we have any ternary operator just for true condition like above example?

    if (num == 1) {
       print(true);
    }