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

10

37

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
      } 

i want to use it like

  foo == 1 || foo == 2 ? doSomething : doSomething
Preordain answered 7/2, 2019 at 6:8 Comment(4)
Use parentheses to group expressions.Naiad
You can use parantheses to use logical "OR" and "AND".Roadster
What is the problem you are seeing? The code foo == 1 || foo == 2 ? doSomething : doSomething is valid, even without extra parentheses.Mcclelland
I would argue a slightly different point - why ternary over if/else? - Don't obfuscate the readability of your code unnecessarily.Handcraft
K
86

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

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

is equivalent to this:

if(foo == 1){
    something1();
}
elseif(foo == 2){
    something2();
}
else something3();
Ketch answered 8/2, 2019 at 1:42 Comment(2)
Nice. so ? and : just alternate alwaysCourland
The ? marks the end of the condition and the : simply means elseKetch
E
15

For three conditions use:

value: (i == 1) ? 1 : (i == 2) ? 2 : 0

Expectoration answered 6/7, 2019 at 5:19 Comment(1)
You dont have to be rude and give negative vote to him. He has given the idea to use multiple ternary operator. I dont see anything wrong.Posthaste
P
4

Try below

(2 > 3)?print("It is more than 3"):print("It is less than 3");
////Prints It is less than 3 to the console
Pronator answered 7/2, 2019 at 6:36 Comment(0)
C
3

For AND try this, // here both or multiple conditions needs to satisfy

if (primaryImageUploaded == true && signatureImageUploaded == true) {
    // status bool condition in true 
    } else {
     // if false 
    }

For OR try this, // here need ONLY any one condition to satisfy

if (primaryImageUploaded == true || signatureImageUploaded == true) {
    // status bool condition in true 
    } else {
     // if false 
    }

Another Dart Syntax

if (100 > 50) {
  print("100 is greater than 50");
 }
Coset answered 4/3, 2021 at 16:21 Comment(0)
J
3

Try this:

foo == 1 ? doSomething1 : (foo == 2 ? doSomething1 : doSomething2)

If you have to include multiple conditions then you should use parantheses

Jenna answered 3/9, 2022 at 6:53 Comment(0)
T
2

it is easy,

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

it can be written thus for OR statement

foo==1 || foo==2 ? do something : else do something

it can be written thus for AND statement

foo==1 && foo==2 ? do something : else do something

both will work perfectly

Treasurer answered 1/6, 2020 at 21:3 Comment(0)
N
2

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
}
Nafis answered 9/6, 2020 at 6:53 Comment(1)
Thank you! This is the cleanest form for my case and reduced around 80 lines of code :DHedges
G
2

Here is an example of the same

Text((managerStatus == "pending")
    ? "Requested"
        : (adminStatus == "confirm")
    ? "Amount credited"
        : "Admin Pending")
Glacial answered 18/6, 2021 at 17:13 Comment(0)
C
1

Simple Multiple check in one condition

if(in_array($foo, [1,2,'other'])) {
    //do something
}
else {
    //else do something
}
Cuisine answered 17/3, 2022 at 17:20 Comment(0)
F
-3
void main(){
  var a,b,c,d;  
  a = 7;  
  b = 9;  
  c = 11;  
  d = 15;  
  print((a>b)?((a>c)?((a>d)?a:d):c):(b>c)?((b>d)?b:d):(c>d)?c:d);
}
Fifield answered 19/3, 2020 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.