Dart Null Safety Introduction | Flutter Null Safety Explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
null safety is often a concept that a lot of beginner programmers struggle with but nonetheless it's a core component of the dot programming language and if you want to create scalable robust less arlone applications then it is pertinent that you understand this concept to its fullest in today's video we're going to be learning all about what null safety is in the context of the dart programming language we're also going to be taking a look at some practical code examples to help you better solidify your understanding of this concept and then finally I'm also going to be sharing with you guys some best practices when it comes comes to working with null safety so that you guys can Implement these within your own programming projects and benefit from having sound null safety within your projects so the first thing that I'd like to talk about before we dive into any code is what null safety is why we use it and what are the advantages of it so to do that I'm actually going to be referring to the documentation that's available for sound n safety on .ev and as you can see it says that the dot language enforces sound null safety and null safety is basically a mechanism you could think of of the D program language that allows us to prevent a lot of errors that might unintentionally occur within our application because of certain variables having null as their value and the last thing that I'd like to mention from this documentation is that with sound null safety all variables require a value and by default do considers all variables non-nullable and I'll come to what non-nullable variables are versus nullable variables but just kind of take this thought and put that into your brain for now and keep that at the Forefront of our discussion and just don't forget it so with that said again I'd like to emphasize that the dart language enforces s null safety so when we write our code we have to conform to the actual ideas of null safety so to begin with the actual coding process now the first thing that we're going to be doing is talking about what non-nullable variables are and then comparing them with nullable variables and then from there we'll talk about more null safety related Concepts so what is a non-nullable variable well whenever we Define a variable within Dart for example I'm defining an integer a and setting it equal to 5 this is an example of a non-nullable variable and the reason I say that is because I've defined a variable a and its data type is integer so this basically tells the compiler that hey a is always going to have a value and a can never be null now if I go ahead and try to set a to null intentionally you can see that it's going to give me an error saying that a value of type null can't be assigned to a variable of type int so I can update the value of a to any other integer value that I'd like for example 10 and that should work but we can't set it to null because we have explicitly told the compiler that hey a is always going to have an integer value within itself so that's pretty much what a non-nullable variable is but when we are programming sometimes variables have to be null either we have to fet some data from an API and we might never get that data so that variable is going to be null or sometimes we might get data and that variable will not be null so in that case that variable is called a nullable variable so let's do this for now let's actually print a to the console and run our program and let me add a semicolon rookie mistake and as you can see after sometime it's going to print five to the console and for those of you wondering I'm using thepad DOD web which is an online ID for development that you can use to run your code within your actual browser so now what we're going to be doing is talking about how we can define a nullable variable so nullable variables are defined in a slightly different manner than the actual non-nullable variable that I've shown you and what we do is that we Define the actual data type for the variable but after that we use a question mark and this basically indicates to the compiler that hey this variable might have an integer or it might be null and then we can give the variable whatever name we want or identifier so in this case I'll do B and then if I go ahead and do this this is pretty much going to B code that is legal code and that's not going to cause any issues so now if I go ahead and print B you're going to see that to the console we should expect null to be printed out and the reason for that is because even if we've defined the variable B we haven't set it to any value so by default it's going to be null if now I go ahead and set the value of b equals to 5 and then do run again and let's do 10 just for some difference you're going to see that 10 gets printed to the console so the way you assign values to a nullable versus a non-nullable variable is the same the only difference is is that a non-nullable variable is always going to have some kind of a value while a nullable variable might have a value and might not have a value so those are the basic concepts or the basic building blocks when it comes to work working with null safety within Dart so now that we have a good understanding of nullable versus non-nullable variables the next thing that we're going to be talking about is the late keyword and why we use it usually the late keyboard comes into play when we're talking about non-nullable variables and by this I mean the variable at the top here which I've defined as a and I'm saying that the data type for a is going to be an integer so this basically tells the compiler that hey in a a is always going to have a value it can never be null but now if I go ahead and I remove this assignment for a and then I try to print a and let's go ahead and try to run our application you're going to see that it's going to give me an error saying that non-nullable variable a must be assigned before it can be used and we haven't assigned a a value so in this case what I can do is add a late keyword here and this is to signify to the compiler that hey I know that a cannot be null but at this time when I'm defining it I don't have a value for it but later down in the execution of my program I am going to have a value for a and I take the guarantee for that so what you can do is ignore the error where a might be null and don't worry about them so now if I do this and run my application you're still going to see that it's giving me an error saying L variable a without initializer is definitely unassigned so what I I can do now is after I've defined my variable set a equals to 10 and if we do that and then I run the code once again you're going to see that now 10 is going to be printed to the console so this is why we use the late keyword when we have a non-nullable variable for which when we're defining the actual variable we might not have a value for it we can specify the late keyword before it and that's going to tell the compiler that hey later down in the execution of my program I am going to assign a value to this variable and it's not going to be null and I'll take the responsibility for that and in the case we do this and you're usually going to see it and we do not assign a value to our variable and we run our application it might run or the compiler might pick it up and give us an error but it might run as well and then if a runtime error occur then it's your fault because you had told the compiler that hey I'm going to ensure that a is going to have a value before we reference the value from a so that's pretty much all you need to know about the late keyword so now that we have a good understanding of the late keyword the next thing that we're going to be talking about are null of your operators how we use them and then I'm just going to be sharing a couple of them with you but they're going to give you enough of an understanding to understand what the rest of them are within the actual do programm language and how to use them so the first one that we're going to be talking about is called the null aare assignment operator and we're going to be using it as follows let's say that I Define a variable in optional X so this basically means that this is a nullable variable and then what I want to do is basically only give a value to X in the case that X is null well in that case what I can do is X and then I can use the null aare assignment operator like so and then I can set it to 20 and then I can do print X like so and if we do that and run you're going to see that 20 gets printed to the console but now if I go ahead and before I use the null assignment operator I set X to 10 then this here is not going to assign a value of 22x because X already has the value X is not null so to the console 10 is only going to be printed so this is why we use the null operator and this operator only assigns a value to its variable in the case that the variable is null so that's pretty much what it is so the next thing that I'd like to talk about after this is the null AIS operator so this operator is used to call a method or access a property only if the variable is not null and I'll explain this with another example let's just say that I have a string optional and I call it name and then what I'd like to do is I'd like to print name. length to the console so what's the length of the string well if I go ahead and do this it's going to give me an error saying that hey the property length can't be un conditionally accessed because the receiver can be null and that's correct we have actually specified the name is a nullable variable it might have a value it might not so here we can't be sure if name is going to have a length property associated with it because it can be n so there are two ways we could approach this the first is that we can use a null access operator and that is by using a question mark before the period and what this basically means is that hey if name is not null then access the length property otherwise don't and that's not going to cause our program to crash so now if I print or I run the actual application nothing gets printed to the console besides null and our application is now now safe but if I go ahead and set name equals to Jon Jones for example and then I run it now you're going to see that it is able to actually access the length on the actual name and then print the actual output for that onto the console so this is how we use a null access operator another way to approach the same problem instead of using a null access operator is to Cast Away the nullability of the variable Name by using an exclamation mark and what this is basically stating is that at this point of time on 94 I know the name is absolutely going to have a value in itself not null so you can be sure that the length property is actually available on the name variable and you can access it and now if I click on run you're going to see the nine will get printed to the console but now if I go ahead and I remove the actual line where I'm setting the name equals to Jon Jones you're going to see that we're not going to get an error anymore because we're explicitly telling the compiler that hey you can skip the null check on this I am sure that the actual name variable is going to have a value but now if I run my application and since name is going to be null it's actually going to crash at run time there we go so I usually recommend that you stick to using null ofare operators an example of that would be the null aare access operator that I've shown you here when you're working with null values or nullable variables and if there is a case where for example a function might require you to pass it a value explicitly then there might be a use case for the exclamation mark there but there there's a another null aare operator that I'm going to be sharing with you guys to use in those cases as well I think that the exclamation mark should be kept as a last resort not as the first option that you deploy when you're writing your application so with that said let's move on to the next null a operator and that is going to be called the null Co escaping operator and I just don't know the name for it but I call the default operator or the default value operator that's what I've given it a name by myself and what this operator basically allows us to do is that it Returns the right hand side value if the left hand side value is null so what does that basically mean well to give you an example of this I'm going to add a print statement and I'm going to say that I'm going to print the name but if I just do this and I run it the code is going to run but null is going to be printed to the console but I don't want to print null I want to print a default value to the console in the case that the name is null well in that case I can use this null a operator which is this double question mark and what this basically States is that hey in the case that this left side value is null then use this right hand side value and here I can say Hussein Mustafa so with that said if I click run again you are going to see that Hussein Mustafa is going to be printed to the console but now before we print the actual statement if I just set the actual name to Jon Jones for example now you're going to see that to the console Jon Jones is going to be printed and the reason for that is because name is no longer null so it's just going to print name if this left hand side value was null then it would have used the right hand side value so that's pretty much some of the most important null your access operators within the dart programming language if you guys want a complete list of all of the null operators that are available within the dot programming language then I'll leave a link down in the description below to this article that's on do tutorial.org where they've given a complete list of all of the null operators that are out there and if you're enjoying this video thus far and would like to learn more about the do programm language then I recommend that you take a look at another video that I've published already on my channel where I share a bunch of cool dot tips with you that are definitely going to elevate your dot programming but here you can see that we have a bunch of different operators some of the ones that have talked about such as the if null operator the null assignment operator and the null access operator but we also have the null assertion operator which I called the exclamation mark I have the null aare casket operator the null aare index operator and the null aare spread operator so some of these are more advanced use cases and if you understand the basics of how to use the topmost operators then when the time comes you'll understand how to use the other ones as well so now with this done the last thing that I'd like to talk about before we talk about some of the best practices when it comes to working with null safety is how null safety kind of works when it comes to working with functions and for that I'm going to be doing the following I have this main function I'm going to be removing all of the content from it and after this I'm going to be defining a function that takes in a value and then returns us a square of it and then within my main function I'm going to say that I'm going to print the result from calling the square function and passing it five so with this done let's run it and you're going to see that 25 gets printed to the console but now what if I want to call the square function but the value that I'm going to give to the function is a nullable value so in this case the value might have a value or it might be null so in this case if I go ahead and I duplicate this print statement and here when I call the actual function I pass null I am going to get an error and the reason for that is because the argument that I've specified here states that the value that gets passed in has to be a non- nple value so we can't pass in null so how can I mitigate this issue well what I'll do is I'll copy this function once more and I'll say that this is going to be called save square and I'm going to say that the value that gets passed in is going to be an INT optional and this way we're going to remove the first error so now we're presented with another issue and that is that we can't multiply value with value and the reason for that is because value might be null and we can't multiply null with something because what would that be so in this case how can we fix the issue well what I can do is do the following I can say that if value is not null then I'd like to return value times value like so and otherwise I'll just return null from the function because if we get a null value given to us then what I'd like to do is return null from the function because we can't square a null but if we do get a value which is not null then I want to return the square for it by doing value times value but now this is going to give me another error saying that hey this function is specified that it returns an integer which is a non-nullable type but here we might have a value returned to us or we might have null returned to us so I'll have to update the actual function definition and say that now our function is going to have a return type which is int optional and now this is going to work so with this I can call the save Square function here and now you can see that I can pass null to the function and if I run it null will get printed to the console and now if I copy this paste it again but this time to save Square I pass in two then I should see 25 null and four and there we go that's how it works so hopefully by showcasing to use these examples you now have a better understanding of how to use null safety and how it actually functions within the context of U writing. code so the last thing that I'd like to talk about is the actual null assertion operator and I had alluded to this before but the null assertion operator basically allows us to assert that a value is not going to be null by ourselves and this allows us to basically bypass the checks that the compiler makes but at the cost that if the value turns out to be null during runtime then our program is going to be crash so I recommend that you use this sparingly because this is a dangerous move and we always want to bite code that is as robust as possible as error-free as possible because that's just going to be ultimately what's best for us and what's best for the end user that's using our application so with that now what I'm going to be doing is sharing with you guys some best practices when it comes to working with null safety the first best practice that I'd like to talk about is the following and that is to initialize variables prly so whenever possible initialize variables as soon as possible most probably or preferably when you actually Define them so for example when I'm declaring a variable int a then it's most preferred that I just give it a value at this point and this is the best thing that you can do the next thing that I'd like to talk about is even though nullable values are great and they are a resource within the do programming language they should be used sparingly and you should always resort to using non-nullable variables wherever possible so that's my next tip that you should use nullable types sparingly only only use nullable types when it's absolutely necessary don't try to just default using them all the time the next thing I'd like to talk about after this is that instead of using the null assertion operator you should try to use null aare operators because null aare operators are able to handle null values more gracefully than a null assertion operator so I highly recommend that you try to leverage as many null of operators as you can in in the case that you're using nullable types and Only Resort to using a null resorption operator in the very worst case so with that that's pretty much it for today's video I hope that you learned a thing or two about null safety and how to use it efficiently within your own programming Journey as always if you enjoyed the video then please don't forget to leave a like on the video and subscribe to my channel so that you get notified every time I release a new video and as always stay happy stay healthy keep learning keep growing and I'll see you guys in the next video bye-bye
Info
Channel: Hussain Mustafa
Views: 498
Rating: undefined out of 5
Keywords: flutter null check operator used on a null value, null safety dart, null aware operator, how to use null safety, how to use null safety in dart, Null safety basics, Null safety in dart, intro to null safety in dart, null safety in dart tutorial, null safety, dart null safety, null safety tutorial, intro to null safety, null safety features in dart, null safety feature in dart, dart, flutter, flutter developers, developers, developer, dart programming, dart programming language
Id: 02FDkRNAfas
Channel Id: undefined
Length: 20min 29sec (1229 seconds)
Published: Wed Jul 03 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.