Ternary Conditional operator in Flutter(Dart)

One of the best features of the Flutter framework is its support for the ternary conditional operator. This operator allows you to concisely express if-else logic in your Flutter code. In this article, we'll take a look at how to use the ternary conditional operator in Flutter.

The ternary conditional operator is a shorthand way of expressing if-else logic. It is often used in situations where a simple if-else statement would suffice, but where using the ternary operator can make the code more concise and easier to read.

String message = isLoggedIn ? 'Hello again' : 'Please sign up';

The basic syntax for using the ternary conditional operator is as follows:

condition ? expr1 : expr2

If the condition is true, then expr1 is evaluated and the value is returned. If the condition is false, then expr2 is evaluated and the value is returned.

Was this helpful?