How To Use If Else In Flutter – Easy Flutter Guide

if else in Flutter

In this Flutter post, we will be implementing if else in Flutter with the help of an easy Flutter example. A step by step explanation will be provided so you can have a clear idea on how to properly use if else in Flutter.

So without wasting anymore time, let’s get right into implementing if else in Flutter.

What is If Else In Flutter?

It is a condition that specified if the condition of first block is satisfied the don’t run the other block, otherwise run it. We will understand the usage of if else in Flutter dart with the help of simple Flutter dart example. So let’s understand it with practical examples.

Implementing If Else In Flutter(Example)

First let’s see the syntax of if else in Flutter. See below code:

Syntax

 if(condition){
     run if the condition is satisfied
 }
 else{
     only run if the above condition is not satisfied
 }

As you can see above, we have two blocks with keywords of if and else. In the if block, you can see that we can specify a condition inside the parenthesis block. Let’s now specify a condition to understand how it practically works.

Example: Simple If Else Condition

Let’s now use a simple condition to understand the working of if else. See the below code:

int i=5;
if(i==5){
    print('The value of i is 5');
}
else{
   print('The value of i is not 5');
}

Output:

The value of i is 5

You can see that we have defined an integer and have given it a value 5. We then have specified a condition that if the value of i is equals to 5 then run the if block, but if the condition is false the run the block of else. You can see that the condition was true and the print statement of if block was executed.

Let’s now make the condition false and see what happens. See below code:

int i=4;
if(i==5){
    print('The value of i is 5');
}
else{
   print('The value of i is not 5');
}

Output:

The value of i is not 5

You can see that the condition was false so it didn’t run the if block, instead it executed the else block.

So this is how you can easily use if else in Flutter. Hope you now have a clear idea on how to properly use if else in Flutter apps.

Conclusion

As conclusion, I have discussed and practically implemented if else in Flutter with proper Flutter example. I would really love to see you implement if else in your Flutter apps and share your experience here. Thank you for your time.

Leave a Comment

Your email address will not be published. Required fields are marked *