 Hey everybody, this is Brian. Welcome to the third Flutter tutorial. Just posted the first two the other day, and as you can see I've already got a few hundred on each one in comments and some thumbs up. So everybody seems to be really interested in Flutter, so we're just going to continue on down the path here. Now, to back up, we have to learn Dart before we can learn Flutter. Looking at the tour of the Dart language, you can see how they kind of... If you go in, they cover variables, keywords, variables, built-in types, which we've covered. So then they jump to functions, which, if you're new to programming, really doesn't make a whole lot of sense. If you remember, a variable is something that will change where a const or a final is something that will not change. A function is a way of organizing that into a logical group, but you haven't yet used variables. You don't really know what to do with them, so we're going to actually skip down to control flow statements. So control flow statements are exactly that. They control the flow of a program. And the first one we're going to cover is if and else. So let's just crack this open here. We're going to make a new Dart console application, and we're going to call this flow one. And while that is thinking, we're going to skip back here. So some of you may be confused and go, why are you jumping to control flow statements? You're not talking about functions. We're going to talk about functions after we deal with control flow statements. The reason being, you know what a variable is, like I said. Now you need to know what to do with the variable. It's something that will change, but how do you handle that change? What do you do when the change occurs? So that's what we're going to really cover today. So let's just get rid of this and get rid of that. Once again, the structure of our application, we have got our main.dart in here. And if you're wondering what this lib is, this is just part of that demonstration code that the... Let me undo all this. It's part of that demonstration code that comes with the template. Really, they're just importing this, which we're going to cover in a future tutorial. But for right now, you can safely ignore it. Now, if you go back to our previous tutorial, you'll see that there is a variable here called arguments. So this main function already has a variable. So something's going in here. An argument is something that is passed to your application when it is first started. Something we're not going to really be dealing with, but just be aware that that's there. That's how you can tell a program, you know, start just normally or start and open this file, that kind of thing. So if and else. Whoopsie. Cannot type. So let's make a variable here. Let's say... Bl is on equal false. So we have a variable. And we're going to say... Let me actually just jump down a little bit so it's easier to read. If and then you have these parentheses is on equal. Notice there's two of those. Now there's a bit of a heated debate in the programming community. And I'll get to this in just a second as I can't multitask today. It has to do with the brackets here. This is what's called a C style language. C is probably the grandfather of all languages. Some are not all languages, but all C style languages. C was meant to make assembly easier. C is actually kind of challenging, but... So you have these brackets and this denotes a scope or a block of code. You notice how there's two of them here. You notice how there's here and here. And actually in this IDE, if you click on one, it shows you where the ending one is. So there and there, there and there. That's your scope. So what we're really going to learn right now is two quick things. First is variable scope and what is the if and else statements. But the religious battle, if you will, is where to put this little bracket. Some people like putting it down here. Some people like putting it up there. Now when I say up there, what I really mean is you take this and you put it on the same line like that. It doesn't really matter where you do it. Some companies have coding guidelines and standards that you should really follow. But just know that you need a start and end bracket to denote scope. So what is scope? I'm glad you asked. Okay. So let's make another variable. Let's call this bear kitty. Equal... I don't know what to name a cat. Let's just call it cat. Why not? So let's run this. See what happens. It is off. We've got a variable. Bull is on equal. Notice there's only one. False. If... So this reads like a statement here. If... And then we have our parentheses. This goes back into arithmetic where you have to multiply and add things. So you'd say 3 plus 2 plus 6 equals, you know, whatever, right? What's ever done in the parentheses is going to be calculated first. That's why it's in parentheses to begin with. So we know that this is going to be evaluated. Now, this is an evaluator. Actually, this is a drumroll, please. Operator. And it is an equality operator. And you can read a lot more here. So you see how there's two of them. Now, why wouldn't you put one? Some of you old programmers know what I'm talking about. If you do that, what you're really doing is you're assigning the value, much like you're doing up here, is on equals, false. So when you do the double, you're saying is on is equal to. You know, if is on is equal to whatever the value. So that's why it says it is off. Now, if we change this to true and run this, it is on. So you can see how you have control flow here. This statement is being executed because it is true. If we flip it back to false, then this statement will be executed along with this variable being created. So let me add some comments here. Inner scope, outer scope. Did I misspell outer? That'll be embarrassing if I did. I don't think that's outer. That's otter. Anyways, not even otter. Man, cold medicine. I'm telling you. So scope anyways. Scope goes in a logical order or you should say top down. So for example is on notice how we can actually access it here. Let's actually do it like this and let's actually just copy this here. Run this. So is on equals false. So we can access this variable in this scope or in this block of code. What happens if we try to access kitty? Notice how IntelliSense isn't helping us out and it's saying object object meaning it doesn't know what the hell we're trying to do here. And it's complaining and it's saying, hey, unidentified named kitty, but we have it here. That's called scope. And true enough, if we try to run this, it's just going to say blah. No top level getter for kitty. What's a getter? We'll cover that in a future tutorial. However, just be aware kitty is out of scope. So where is kitty right here? Where's the scope right here? We can access kitty just fine as long as we're in that scope. Once again is on this variable, probably horrible variable name, is in the outer scope. So it's available to anything in a sub scope, sub scope being a scope declared within this larger scope. That gets confusing. So let's go over that one more time. We have our outer scope here. In that outer scope, we have subscopes. Kitty is defined within a sub scope. So it is not accessible in the outer scope. However, variables described in the outer scope are available on the inner scopes. Is that even the right way to say an inner scope? Somebody correct me if I'm wrong. But if we simply move this print statement up here, we can see it runs just fine. So that is a very quick lesson on if and else and is on. What's the difference between if, else, and? Scroll down here. Whoa. We went way too far. Else, if. You'll see this a lot. What's really the difference here? I just stuttered. That was embarrassing. Anyways, the difference between if, else, if, and else. Let's figure this out. Let's just do the magic of copy and paste. Change this around a bit. Actually, no, I want to keep this as an example. Let's say int test equal one. Why not? Why are you indebted? I don't like you. All right. So if test equal, I'll say two. Now, if you're going to choose a bracket style, you should be consistent in that style. And you should probably put it in the print statement, Brian. All right. So we've got our little else here. And we can even break that down further like this. I'm kind of like an indentation snob, if you will. I like it a particular way. Else. And we're going to just say, now, from our scope conversation, we know that we can access this variable because it's in the outer scope. So let's run this. So test is one. So this got executed, not this. So let's just copy this again. And see what happens. Notice how we're getting an error already. Expected a statement. What's going on here? Line 30, position three. So we're at line 30, position three, because we're three spaces in. Unexpected token else. But if we say else if up here, what happens? It breaks. So let's look at this and see what we're doing wrong here. We have our if. We have our else if. Now, this is what we're missing here. So we have to notice how all the errors suddenly clear up. And let's just so we know this. So test is one, because test is one. Confused yet? Don't be. It's just a way of chaining if statements. You have an if statement here and if statement here and if neither of these actually apply, then do this. That's really all that does. Now, there is an easier way of doing that, or at least a more elegant way of doing that. But before we cover that, I want to talk about comparisons in the if statement itself. If you read their disclaimer, it says remember, unlike JavaScript, Dart treats all values other than true as false. See Booleans for more information. Sometimes people will say things like test equals zero and then instead of that, they'll just say test, if test. And you notice how it must be a type of bool. So it's got to go into a true or false sort of statement. It's pretty easy to do that though. So now we're going to cover what's called the switch statement. It's called switching case. And I think that'll probably wrap up for this tutorial. So this looks just horrendous. What is this thing? Okay, let's break it down a little bit. You've got switch. Now think of this as a giant switchboard on off, right? You've got a dozen switches in front of you and you can flip each one on and off. So here's the variable. So we've got very command. And then we've got our evaluator, the parentheses. And then we've got a scope. So within this scope, we've got a bunch of cases. Now think of these cases as shorthand for if else. We're going to rewrite this whole thing. So using the test, switch, test, and there's our scope. Notice how what we're not doing is testing for equality or inequality. Like we're not saying, you know, not equals or is equals. We're just saying we're going to switch on this test. Now we've got a bunch of switches. And we'll say case zero break case. Let's actually back out here. Case one break. What is going on here? Well, let's find out. The test is not one test is zero. Derp. I hate having a cold. I feel so just out of it when I have a cold. It's finally going away. But man, what a long road this has been. All right. So you can see up here, our variable is zero. So what's going to happen? And let's actually define this out here. Switch. That way we can really see what's going on when we print these out. Run this bad boy. You can see switch test is zero. So really what we're doing is pretty much the same thing. It's just we don't have all this if else gobbly gook. So this monstrosity right here is the same thing as saying case equal one inside of a switch. So here's our switch. Here's our switch. Now, one thing you should know is there is an invisible scope here that you don't really realize. So let's just say bare dog equal Sam. And then we're going to print Sam. Notice how Sam is going to say undefined object. And if we try to run this, of course it's going to break. Oh, no, it actually didn't break. Interesting. Probably because it didn't execute. Let's do this. Yeah. Now it's going to break. So that's how you'll find some errors in your code. Like if it's working, just find one time, but then not working another time, check your scope. The invisible scope is between the case and the break. Think of the invisible brackets being here and here. Each one of these cases is a little switch, like a light switch, right? So we've got two of them here. We're declaring this variable inside of this switch or this case. It's not declared here. Therefore, this code gets executed when it's zero. This code gets executed when it's one. So if this code is executed, this variable is never created. You see the catch 22 now. So if you wanted that, you would either have to make that in each one or better to do it up above. Now let's get rid of that and get rid of that. And let's see if this works. No top level getter for SAM declared. So you'll have to understand that scope here is a little wonky and a little crazy sometimes. So you just definitely want to make sure that you declare your variables in the proper place. Now, why isn't this working here? That's a good question. And honestly, I don't know. In other languages, this works just fine. So why is this not working? Probably because I have the wrong variable name altogether. So let's retest all of that. That was embarrassing. So let's print dog, dog. I may be wrong. I'll be flabbergasted if I'm totally wrong here. All right. So back here test is one. So this will execute. Yeah. All right. So and if we put this here, it suddenly works. But it would be much better to actually put it up above right here. All right. Sorry for that little confusion. That's also a good conversation we should have is variable names. It doesn't exist. You're going to get an undefined error as you just saw. So that in a nutshell is the switch statement. But there is one little question we have. One naggy little question. What is default? You notice how it's case, case, case, case, default. And what does break mean? Well, let's try something here. Let's actually make a case three print. I'm actually trying to hurry because I'm supposed to take my daughter out to lunch and starving, which is probably why I'm making a lot of mistakes here. And then we're going to say default. No idea. Wow. I can't even spell no today. I'm going to go get some food here right after this video because I am just dying. I'm going to go get some hot wings, man. I'm hungry. All right. So we've got our, our cases here. We're just going to put a hard return so that it's easier to read here. So we've got our case here, our case here, our case here. And default, which is actually a case. So let's change our test to a different number or something that we're not accounting for and run this. So that says no idea. So default is basically a giant else. It's this else statement here. We're going to switch this back to let's say, did we totally skip two? We did. That's just odd. Somebody's probably yelling at their monitor going, there's no two. Give her a dog here. All right. So we've got two. Let's just get those in there the way they should be. Put that in as a two. Run it just to double check. Test is two. Now let's talk about these breaks. Let's actually comment these out and see what happens. Notice how last statement of case should be break, continue, rethrow return or throw. What does that mean? We're going to get into that a little more in depth in future tutorials, but right now you need to understand what's happening. Break is like hitting the brakes on your car. It stops execution of the code. If we change break to continue, continue statement is which must have a label or as a target. What that's telling us is that we could continue the flow of execution if we wanted to go somewhere else. I actually don't know how this works, but let's say continue three. Nope. Continue case three. I'm sure there's a way to do this. I just don't know. Continue case is not a word. Yeah. I'll figure that out in a future tutorial, but basically just understand what break is doing is actually breaking. Let's get rid of this again and see what it's saying. The last statement of case should be break, continue, rethrow, return or throw. Let's try return because I know that'll actually work. What is the difference here? Case three, the last statement of the case, blah, blah, blah. Let's turn that. There we go. Now it's working. What's going to happen here? Pop quiz, everybody. Based off the current code, test is equal to two. What do you think is going to happen? Is it going to print done or is it not based off this code? Let's find out. There's no done. If we switch this and rerun it, see how done suddenly appears. That's what's going on. Break will stop execution, but not return. Return means it actually exits the entire function. Return is actually returning out of this entire function, actually out of this function right here. Switch isn't really a function, but I'm going to call it a function. Don't tell anybody. It's going to exit out of the switch statement and then hit print. When in doubt, use break, but just understand that that break is there for a reason. Some languages have what's called fall through, so you could actually say this and then this. If it's case one, break, case two, we want it to do this and this and this. Just understand that is a thing and we need to prepare for it, which is why the break statement is there. That is a lot of talking and typing. I'm going to go get some hot wings with my daughter. Thank you for watching, everybody. The source code for this and other tutorials will be out on my website, voidromes.com. There is a link to GitHub. Let's just go out there. You can click on repositories. What I do is I have individual repositories, but then I have a master repository that has the individual lessons in there. These are called submodules. It may not actually download with a full zip. You may have to actually get and pull the whole thing down. That way people can contribute to the code. If I make a glaring mistake and it is out in the world forever, somebody else can submit it. If you found this even remotely helpful, my website and all this code is actually run purely off donations. You don't have to, obviously, but if you are a company and this is helping your employees learn, you don't have to donate anything that goes above and beyond what I used to run the website. It actually gets donated to charities. Other than that, thank you for watching.