Speaker A
Hello everyone, and welcome to chapter four of this Flutter course. In the previous chapter, chapter three, we talked about keywords, data types, constants, variables, and some basics of functions. In this chapter, we are going to, as you can see on the captions here, focus on control statements and collections. Now, I'm assuming you're following these chapters chronologically, so that you're going one by one. I'm assuming then that you have your Dart project set up and that you've got some sort of a simulator or emulator running. So, without further ado, let's have a look at—I'm actually gonna bring up the captions here, and then I'm gonna put my face at the bottom there and bring up our project that we created in the previous chapter up here. So, this is running at the moment. So, if I do a command S or control S on Linux and Windows, then as you can see here, there is a run button, and then it's just gonna do a hot reload, which in turn calls this build function, which we don't yet know really how it works because I haven't really explained it. But what we did, we just called a print function here and then our getFullName function. So, what I'm gonna do here is let's remove this code from the build, and let's remove everything that we wrote here as well. So, keep the main function as you can see here, and then keep your StatelessWidget, which we're gonna talk about later. Okay, so instead, go and create a new function that is called void test. All right? And then in the build function, just say test, all right? Just like that. The purpose is that in this function, we're going to write most of the code in this chapter. So, we are going to basically test some code. That's the purpose of this function. So, let's talk about if and else if and else. They are control statements in most programming languages that allow you to branch your code based on a condition. So, if we, for instance, in here, create a final variable whose value cannot be changed, if you remember from the previous chapter, whose value cannot be changed after it's been assigned to. Let's say final name is foo, all right, and end it with the same semicolon. Now, an if statement basically allows you to check the value or a condition to be of a certain value or to be true or false, and then it allows you to execute some code. And then you can also append an else statement to it, which gets executed if the code or if that condition is not met. I'm going to show you how that is written. The syntax for an if statement is just if, and if you think is a keyword, so you would say if, and then you would open and close parenthesis, and then you would put curly brackets like this. That's the syntax of if. And in the parenthesis, you would put your condition. So, I would say in this case if name, and I would like to, for instance, check if name is foo. Now, we haven't come to operators yet. We're going to talk about operators actually soon. But this is, as you can see here, we're saying final name equal to foo. This is kind of like an assignment operation, and that is an operator in Dart. It's called an operator. So, there is also, and since this is an assignment, we don't want to actually assign anything to name. We want to check if it's of a certain value, and that is with this conditional operator equal equal. And then you would just say, okay, if it's foo, then for instance print yes, this is foo, and then a semicolon to end it. However, if this condition is not met, you may want to execute another piece of code, and then you can just do else like that, and then this would, you would just say no, this is not foo, all right, and a semicolon. So, I'm just gonna do command S, and you can see the value or the string yes, this is foo printed to the screen or to the terminal here or to the debug console. You can have multiple statements in here, so you can have more print statements in here. You can just do anything you want basically in here, and the same goes for else. Now, there is a possibility to write if statements kind of without the curly brackets, but you don't see them that often, and I recommend that you actually always try to, as a convention, do with curly brackets if you can. However, it is possible that inside a code base that you will work on with your colleagues, it is decided that it's okay to have if statements without curly brackets where they are very simple and one-liners basically. So, let me show you an example of that. You could just say if name is foo, and you would just say print yes, this is foo, and this is completely accepted as well because it's a one-liner. But if you then say, okay, I want to have another line print hello world, you can see it goes to the next line, meaning that this line right here will be executed no matter your condition up there. So, this is completely detached from your if statement up here. Okay, and I just realized that it is quite important for me to be able to show line numbers, and by default, your editor probably has line numbers enabled. I usually disable line numbers when I'm working myself basically, but when talking about code with other people, it's quite useful to have line numbers because I can then tell you, oh, look at line nine or look at line ten. Okay, so that's the basic of if and else. Now, you can also have if right after else. So, an example of that is, for instance, to say if name is foo, and then you do some code, all right? You say print yes, it is foo, not food. After the curly bracket, then you would say else, and then you can then say another if, else if, all right? And then after if comes the parenthesis and a curly bracket if you remember from the if before. It is the exact same syntax as it is here, so there's pretty much no difference. You can see if, and then if, and then you put your condition in here. You would just say if name, and then you may just want to say is not, and that's another operator. It's basically an is not operator, so it's checking to make sure that this name is not of the value that you provide to the right-hand side. And then you can say bar and print this value is not bar. Else, and then you can wrap it up, and you can say print I don't know what this is now. I'm so glad I actually accidentally ran into this issue. As you can see, now the editor is kind of going crazy, and Dart's not understanding what this statement is. And as we talked about it previously, strings in Dart are preferred to have to be kind of like created with single quotes. However, if your string in itself contains a single quote, for instance, in this case, the word don't contains a single quote, then Dart kind of gets confused like, okay, does the string end here? You can see it's up until this point is green, so it's thinking, oh, the string ends here, but what is all of this then that follows? If you have that problem, there are two ways of fixing that. Either you escape your single quote, and that is a software kind of development lingo escape kind of. If you hear that, it pretty much just means that you are kind of wrapping it in a way that Dart understands that you don't mean it literally. You don't mean that I want to end my string here, except that this needs to be escaped and put in the string as its own value basically. It's kind of difficult to explain, but I think you know what I mean. So, that's one way of doing it. It's a little bit dirty, so in case you run into this issue, it's best to actually wrap your entire string in double quotes, and this way then Dart understands that whatever comes in here is okay. Like a single quote is completely okay unless you actually want to have a double quote in your string that is enclosed with double quotes again. So, you'll see if I put a double quote here, then Dart goes crazy. So, you can escape it with a backslash like that. So, if you have single quotes and double quotes in your string, then you will have to kind of pick a convention either you enclose the entire string in single quotes and escape your single quotes inside that string and let the do—











