Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use short if in flutter?

How to use short if in flutter

this code can use:

1 + 1 == 2 ? print('check true') : print('check false');
Ans. print('check true')

but I want to do this:

1 + 1 == 2 ?? print('check true');

Why code can't print check true?

like image 418
Thanapol Thong-art Avatar asked Jan 26 '26 00:01

Thanapol Thong-art


1 Answers

Simply

1 + 1 == 2 ? print('check true') : print('check false');

is equals to

if(1+1 == 2) {
    print('check true');
else {
    print('check false');
}

and

1 + 1 == 2 ?? print('check true');

is equals to

if((1+1 == 2) == null ) {
    print('check true');
}
like image 145
prahack Avatar answered Jan 28 '26 17:01

prahack