Dart Flutter Ternary Operator

Created At: 2024-03-05 17:10:38 Updated At: 2024-03-15 06:19:21

Here we will see how to use Dart Flutter Ternary operator instead of else if statement. Ternary operator is convenient and easy to use. Ternary operator specially useful when you use widgets and want to check with condition.

Let's see what is Ternary operator. Ternary operator is ?: . Here we write ? and : to represent the operator. When we use Ternary operator we use both ?(question) mark and : (colon) separately. Before we proceed let's take a look at conditional.  

First take a look at the below else if statement

From the above picture we know the first print() statement would be shown on the console. Now there is a easier way to do this trick. 

We will use ternary operator to replace else if statement. 

See how we replaced else if statement with ternary operator.

Comment

Add Reviews