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

后端 未结 6 1020
逝去的感伤
逝去的感伤 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:50

    If you're referring to else if statements in dart, then this ternary operator:

    (foo==1)? something1():(foo==2)? something2():(foo==3)? something3(): something4();
    

    is equivalent to this:

    if(foo == 1){
        something1();
    }
    elseif(foo == 2){
        something2();
    }
    elseif(foo == 3){
        something3();
    }
    else something4();
    

提交回复
热议问题