如何在扑镖中使用具有多种条件的三元运算符? [英] How to use a Ternary Operator with multiple condition in flutter dart?

查看:57
本文介绍了如何在扑镖中使用具有多种条件的三元运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 OR和 AND两个或多个条件下使用三元数,如

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

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

我想像

  foo == 1 || foo == 2 ? doSomething : doSomething


推荐答案

如果您指的是else dart中的语句,然后使用该三元运算符:

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

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

等效于:

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

这篇关于如何在扑镖中使用具有多种条件的三元运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆