How to use a Ternary Operator with multiple condition in flutter dart?

后端 未结 6 1073
逝去的感伤
逝去的感伤 2021-02-03 10:28

how to use ternary if else with two or more condition using \"OR\" and \"AND\" like

    if(foo == 1 || foo == 2)
     {
      do something
      }
     {
      e         


        
6条回答
  •  爱一瞬间的悲伤
    2021-02-03 10:46

    EDITED

    The original answer has run a little bit of from the question asked. Below is my edited answer.

    To use ternary operator

    (foo == 1 || foo == 2) ? doSomething() : doSomethingElse();
    

    For my cleaner approach

    {1, 2}.contains(foo) ? doSomething() : doSomethingElse();
    

    ORIGINAL

    The cleaner way for me is

    if ({1, 2}.contains(foo)) {
      //do something
    } else {
      //do something else
    }
    

提交回复
热议问题