Programming in Dart: Fundamentals

Apr 26 2022 Dart 2.15, DartPad, DartPad

Part 1: Fundamentals

6. Set Conditional Values

Episode complete

Play next episode

Save for later
About this episode
See versions
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 5. Use Logical Operators Next episode: 7. Challenge: Play with Logical Operators

This video Set Conditional Values was last updated on Apr 26 2022

At this point, you should feel pretty good about working with logical operators. You’ve seen how combining them can produce a true or false value. But what if wanted to set a value based on the result of our logical operators? Often times, you’ll want to set a variable based on a condition.

For example, say you were going to a luncheon that had three types of deserts: cookies, sundae, or a cupcake. You set the variable based on a user’s preference.

For these, we use an if-statement. If you went through the Your First Flutter App course, you should be right at home with if statements. They evaluate on a logical operator. If the expression in the parenthesis is true, then the first pair of braces is evaluated. You can provide many different conditions and one optional catch-all condition in the case that nothing was true.

By using if statements, we can set variables based on conditions. Unfortunately, this won’t play well with our constants. Think about it - a constant can’t change. In dart, once you define a constant, it is defined for life. Yet, if you define a constant in the braces, that constant will only exist in the braces. It is inaccessible outside the braces.

Well, you could fallback to using variables, but some things shouldn’t change as it could break your program. For instance, imagine changing the hours in a day to twelve instead of twenty four? That could wreak havoc in your program, so it’s to leave the hoursInDay as a constant.

Thankfully, Dart provides the final modifier. By defining a variable as final, you are defining a constant. Yet you can dynamically assign a value to it, but only once. Afterwards, it is treated exactly like a constant. Let’s put final to use.

Demo 1

We’ll get started by defining a message that we want to print out to console. So lets define a variable for the message to be presented.

String message;

Notice that we didn’t use var, but instead, we defined the type. If we defined a variable with just var, the compiler will have no way to determine the type.

var message;

Dart still gives it a type. It’s a dynamic type which means any type. You use this type in very specific situations and it is not meant to be a catchall type.

String message;

Now’s let’s define some variables for Chris.

const chrisHasPerfectAttendance = true;
const chrisGrade = 49;
const meritAwardGrade = 90;

With the variables all setup, let’s set up the chrisIsMeritStudent variable.

const chrisIsMeritStudent = chrisHasPerfectAttendance && chrisGrade > meritAwardGrade;

And we see, to no one’s surprise, that Chris’ grade is not greater than the merit award grade, so it doesn’t matter that he has perfect attendance: the value of this entire expression becomes false. Let’s create a simple if statement to set a congratulatory String, if Chris is a merit student, like so:

if (chrisIsMeritStudent) {
    message = 'Congratulations';

Notice we get an error when we try to print out the message variable. This is because a value may not have a value set to the variable. If Chris isn’t a merit student, the message variable won’t be set and thus won’t have a value. We cover this in the topic known as Null Safety which you’ll learn in the next part of this course. Let’s add an else clause to set the value.

else {
    message = 'Keep studying';

By adding an else clause, we ensure that a value will be set in the message variable and thus, the error goes away. Now let’s run the dartpad. In this case, Chris isn’t a merit scholar so the Keep Studying message is printed out.

You’ll be conditionally setting variables all the time.

We do it so much there’s actually a shorthand way of doing it. It’s called the ternary operator and at first glance it looks weird.

First we define a variable that will hold the result.

After the equals sign, we provide an expression in the parenthesis. Like all expressions, it must resolve to a true or false value.

After the expression, you provide a question mark. Think of this as the word then followed by a value.

The colon can be thought of the word else followed by the else value. So like an if-statement, you’d read it like ‘if the current player is one, then set the name to player one else, set the name to player two.’.

It looks weird but it is short and concise. Now, you can actually nest ternary operators, but these quickly become unwieldy and hard to read. In short, if find yourself needing to do that, it’s better to defer to an if-statement instead. Let’s see it in action.

First, let’s set a variable to hold Sam’s grade.

const samGrade = 99;

Now let’s write a variable to hold the name of the better student. It will either be Sam or Chris. First let’s define a variable. We’ll make it a constant.

const betterStudent 

Next, we’ll assign it based on the expression of samGrade being larger than chrisGrade.

const betterStudent = (samGrade > chrisGrade)

If it’s true, we’ll assign Sam otherwise, we’ll assign Chris.

? 'Sam' : 'Chris';

Now, let’s print out the result.


Now if we run it, we get the message Sam. Nice work!