Programming in Dart: Fundamentals

Apr 26 2022 · Dart 2.15, DartPad, DartPad

Part 1: Fundamentals

06. Set Conditional Values

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 05. Use Logical Operators Next episode: 07. Challenge: Play with Logical Operators

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

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 you wanted to set a value based on the result of our logical operators. Oftentimes, 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 desserts. Cookies, sundaes and cupcakes. You set a 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 express in the parentheses is true, then the first pair of braces is evaluated. You can provide many different conditions in one optional catchall 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 of 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 fall back 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 12 instead of 24, that could wreak havoc in your program. So it's best to leave that variable 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. We'll get started by defining a message that we want to print out to the console. So let's define a variable for the message to be presented. Notice we didn't use VAR, but instead we define the type. If we defined a variable with just VAR, the compiler will have no way to determine the type. Dart still gives it a type. It's a dynamic type, which means any type. You'll use this type in very specific situations, and it's not meant to be a catchall type. Now, let's to find some variables for Chris. With the variables all set up, let's set up the chrisIsMeritStudent variable. And we see, to know and surprise, that chrisGrade is not greater than the meritAwardGrade. So it doesn't matter that he has perfect attendance. The value of this entire expression becomes false. Let's set a simple if statement to set a congratulatory string if Chris is a merit student, like so. Notice, we get an error when we tried 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. 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 this in 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 a little weird. First, we define a variable that will hold the result. After the equal sign, we provide an expression in the parentheses. Like all expressions, it must resolve to either 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 an 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's short and concise. Now, you can actually nest ternary operators, but these quickly become unwieldy and hard to read. In short, if you 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. Now, let's write a variable to hold the name of the better student. It will be either Sam or Chris. Let's define a variable, we'll make it a constant. Next, we'll assign it based on the expression of samGrade being larger than chrisGrade. If it's true, we'll assign Sam, otherwise, we'll assign Chris. Now, let's print out the result. Now, if we run it, we get the message, Sam. Nice work.