Flutter Common Control Flows And Statements 😎

Introduction

Hi, folks! In this article, we will learn some expected control flows in flutter 3. You already know some of these in other programming languages. Flutter also uses the same control states to validate boolean functions checking conditions. If you're new to flutter, please check out my previous article. This will be more helpful for your learning. Let’s get started.

If Else 😃

The "if else" condition is just a block of code that can evaluate whether a given value is true or false. Here are if statement syntax. For example, we'll use some odd or even examples. Any number fully divided and remaining is 0. That number is even, and remaining some numbers are odd. I created an odd-even function. If you are new to functions in flutter, please check out my previous article.

if (boolean expression) {
    //executed if the value is true
} else {
    //executed if the value is false
}
void main() {
    oddEven(4);
}
void oddEven(int value) {
    if (value % 2 == 0) {
        print('$value is even');
    } else {
        print('$value is odd');
    }
}

Ternary Operator

The ternary operator is nothing but another way for an if else statement to check conditions. Please see the first part, as it is the same as if and else. The question mark represents whether the value is true. Execute after the question mark section, else if the value is false, execute after the “:” part. 

Condition ? expression TRUE: expression FALSE

void main() {
    oddEven(5);
}
void oddEven(int value) {
    final type = (value % 2 == 0) ? 'even' : 'odd';
    print('$value is $type');
}

While Loop

While loop is nothing but an execution of the true condition for a specific range. Here are some examples using a while loop. In this section, I use the list here. If you are new to the list, please check out my previous article about the list. In this block of code, count the total value of the given list.

void main() {
    final values = [1, 2, 3, 4, 5];
    print(sumOfValues(values));
}
int sumOfValues(List < int > values) {
    int i = 0;
    int result = 0;
    while (i < values.length) {
        result += values[i];
        i++;
    }
    return result;
}

For Loop

The For loop is similar to the while loop. Here I've reconstructed the same code from the previous while loop to for loop. 

void main() {
    final values = [1, 2, 3, 4, 5];
    print(sumOfValues(values));
}
int sumOfValues(List < int > values) {
    //int i = 0;
    int result = 0;
    for (int i = 0; i < values.length; i++) {
        result += values[i];
        //i++;
    }
    return result;
}

Enumerations

An enum is a class that is used to represent a fixed number of constant values. Enumerations are more similar to switch statements.

void main() {
    printStatus(Authendication.login);
    printStatus(Authendication.invalidCredentials);
    printStatus(Authendication.accountNotFound);
    printStatus(Authendication.forgotPassword);
}
enum Authendication {
    login,
    invalidCredentials,
    accountNotFound,
    forgotPassword
}
void printStatus(Authendication status) {
    if (status == Authendication.login) {
        print('Please login to continue your Account');
    } else if (status == Authendication.invalidCredentials) {
        print('Your Credentials Does not Match');
    } else if (status == Authendication.accountNotFound) {
        print('Your Account Not Found Please register new');
    } else if (status == Authendication.forgotPassword) {
        print('Click here to change your password');
    }
}

 

Switch Statement

The Switch Statement is used to evaluate true cases, similar to enumerations, but in a switch statement we must alter some code to transform enum to switch. So Declare the condition in the case, and add the execution code inside the case. Don't forget to add a break after your case ends.

void main() {
    printStatus(Authendication.login);
    printStatus(Authendication.invalidCredentials);
    printStatus(Authendication.accountNotFound);
    printStatus(Authendication.forgotPassword);
}
enum Authendication {
    login,
    invalidCredentials,
    accountNotFound,
    forgotPassword
}
void printStatus(Authendication status) {
    switch (status) {
        case Authendication.login:
            print('Login to Your Account');
            break;
        case Authendication.invalidCredentials:
            print('Invalid Credentials');
            break;
        case Authendication.accountNotFound:
            print('Account Not Found');
            break;
        case Authendication.forgotPassword:
            print('Forgot Password');
            break;
    }
}

Conclusion

In this article, we discussed some common statements and loops. Please check my previous article for more basic functionalities in dart languages. Next, we will dive into some advanced concepts in dart. Thank you.


Similar Articles