How To Easily Use Flutter Switch Statement

Flutter switch statement

In this Flutter post, we will be understanding how to use Flutter switch statement by using a very simple and easy Flutter example code. I hope you will easily customize Flutter switch statement in your own Flutter apps after reading this post. So why wait more? Let’s both of us get right into implementing Flutter switch statement.

What is Flutter Switch Statement?

Flutter switch statement is a very powerful and easy way when you want to compare a value with multiple conditions. Just make sure that the value that is being checked and the value in conditions, both should be of the same type. Let’s understand it using an easy Flutter example.

Implementing Flutter Switch Statement(Easy Example)

First let’s understand the syntax of Flutter switch statement so we can have a better understanding of how to use that switch statement. See below code:

Syntax Code

   case condition1:
     print('Print this value');
   case condition2:
     print('Print this value');
     print('Default value is printed');

Explanation of Switch Statement Syntax(All Steps)

See below steps for explanation:

  • First the switch keyword is used.
  • Inside parenthesis, the value that will be checked should be used.
  • Curly braces defines the the body of Flutter switch statement.
  • There are cases(conditions) in the body of switch. It starts with case keyword, then the value which will be compared to the above mentioned value. Use the colon(:) to end this line.
  • The body of that specific case will come in next line with come indentation. You can specify any action here.
  • Use the break keyword to break the switch statement if that case satisfies the conditions. Close this break with semicolon(;).
  • After specifying all the cases, use a default value if none of the cases satisfies the condition. Use default keyword and close this line with colon(:). Then use the print or whatever function you like in the next line with some indentation.
  • Make the indentation same for every case in this switch statement.

I know its little hard to understand it with just a theory. Don’t worry, we now will use a practical example using the above steps. See below example:

Example: Simple Flutter Switch Statement

We will be defining an integer variable and give it some value, then we will check whether this value is present in any of the case of Flutter switch statement. If not found, then print the default statement. See below code:

int i=4;

  case 2:
    print('value is 2');
  case 3:
    print('value is 3');
  case 4:
    print('value 4 is found');
    print('Specified value not found'); 


value 4 is found

Let’s now pass it a different value which is not present in any of the case of switch statement. We will be using the same above switch for demonstration. See below:

int i=5;


Specified value not found

You can see that the default was executed when the value was not found in cases.

So this is how you can easily check multiple conditions of the same value with less code. You can use String value or other type, just make sure that the type of these values are same. If you still have any questions related to the customization of Flutter switch statement, then do let me know in the comment section. I would be really happy to answer all.


To conclude, I hope you now have a proper understanding of how to use Flutter switch statement in your own Flutter apps. Don’t hesitate to comment down your thoughts about this post. Thank you for reading this post.

Leave a Comment

Your email address will not be published.