How to use ternary operator in Flutter/Dart

Asked

Viewed 5,735 times

2

How to do the if/else in a row on Dart/Flutter?

Type: variavel ? null : function() : function2()

  • variable == null ? Function() function2()

1 answer

6


You can do it like this:

Comparison ? Value if true : False value;


Ex.:

void main() {
    for (int i = 0; i < 5; i++) {
        var result = i % 2 == 0 ? print("$i - par") : print("$i - impar");
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.