Learn Just Enough Dart For Flutter: Part 3

A beginner’s guide to learning dart and flutter

Manthan Gupta
TheLeanProgrammer
3 min readApr 21, 2020

--

In Part 2 you learned all about functions and different variations and I hope you now know about functions. So, let’s move forward and learn some control flow statements.

Dart Control Flow Statements

If-else

If a condition is true then this block executes else the other one executes. That’s the one-line explanation of the If-else statement. Let me explain this with an example, consider that you want to know if a number is even or odd then we will calculate its mod and if its mod is equal to 0 then print even else print odd. Still a little confused? Check out the code

For loop

For loop is implemented when you want to repeat some tasks for a set amount of time. For example, if you want to print the first 10 natural times then you will initialize a variable to 1 and a condition that the variable should not increase greater than 10 and then an incrementing statement so that the value of the variable increases every time the loop executes.

While Loop

A while loop will execute the block of code repeatedly as long as the test expression is true. I will show you how to print the first 10 natural numbers using a while loop.

Do While Loop

The Do while loop first executes the block of code and then tests the condition. You are executing the block of code at least once even when the condition turns out to be false in the first test itself. Let’s try to print the first 10 natural numbers with the do-while loop.

Switch Case Statement

The switch statement evaluates an expression, matches the expression’s value to a case clause, and executes the statements associated with that case. The value of the variable is tested against all cases in the switch. If the variable matches one of the cases, the corresponding code block is executed. If no case expression matches the value of the variable, the code within the default block is associated.

Exception Handling

Your Dart code can throw and catch exceptions. Exceptions are errors indicating that something unexpected happened. Refer this to know types of Exceptions.

Throwing an exception

Let’s first all learn how to throw an exception.

But how do we handle this exception when it occurs?

Catching and Handling Exceptions

You write your block of code under the try keyword. When you know what type of error is going to occur then we use on keyword to handle it. And if we don't know what error is going to occur then we use catch keyword to handle it. I will show the use of both the keywords in the code below.

What if you want to execute a block of code irrespective of the exception is thrown.

Finally

Finally keyword allows you to execute a block of code irrespective of the error that is thrown.

You just learned how to throw an exception, how to handle it, and execute a block of code irrespective of anything which is very cool.

That’s it for this part if you like this blog do click on the clap button multiple times. Leave some constructive feedback if you think there is some room for improvement.

Click here to read the next one or click here to read the previous one.

Don’t forget to follow The Lean Programmer Publication for more such articles, and subscribe to our newsletter tinyletter.com/TheLeanProgrammer

--

--