Null Safety Explained | Why it is Awesome!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I just recently revived a project at work which I built in flutter. Going in I was excited thinking I could finally use null safety, and boy was I disappointed.

The migration process was clunky and in the end my very modest set of dependencies didn't turn out to be ready, and then I finally stumbled upon the official recommendation that suggested you still stay away if you're building apps, but to use it if you're building a library.

This, combined with having to use the awful, awful codegen for stuff like JSON and DB serialization, and the language's type system otherwise being very limited, made me decide that I would rather rewrite the whole thing in typescript and use react-native, but unfortunately Flutter has some minor advantages for what we're trying to do which made react-native a bad choice in the end.

I guess I don't have anything specific to say other than that I can't wait till null safety is a real, usable thing.

👍︎︎ 5 👤︎︎ u/saevarb 📅︎︎ Feb 13 2021 🗫︎ replies

It's awesome when you can use it.
But when you have dependencies that prevent you from using it, then that's not awesome.
This is called a wasted migration time.
Because everything needs to be migrated back, otherwise it won't work.
In all other cases, it's awesome.

👍︎︎ 3 👤︎︎ u/andrew_mezoni 📅︎︎ Feb 13 2021 🗫︎ replies
Captions
so you've probably been hearing a lot about this no safety thing coming to flutter and you've probably seen the flutter team pushing a lot of packages and everybody to upgrade to no safety but what is null safety why do you need it and how to use it coming out right now hello friends my name is titus and on this channel we focus on exploring development topics and specifically with flutter and today's topic is going to be no safety with flutter but there's really two types of flutter developers first type that heard about null safety and were responsible and started using it and learning about it and upgrading their packages everything with no safety and a second type that have procrastinated no safety and are waiting until they're forced to finally use it when they will learn about it and only then after being forced they will start using it so i definitely fall into that second category of people and that's actually why i'm making this video to force myself to use it and then hopefully teach you guys so you stop being lazy and start using it yourself so now that i've learned about it i must say i'm really excited for null safety to come to stable and has become the normal way of development makes development a lot safer it gives us a lot of protection against bad code and just overall will improve your code a lot so let's go over the main concepts and things to know about null safety now when it comes to developing i feel like a lot of developers have the notion that no is bad and you get a lot of null errors and it makes you frustrated but in actuality no is nice it can actually be really useful when you take care of it and protect your code from it for example let's say you're writing some code and you want to have a variable called favorite sport but let's say one user doesn't have a favorite sport so you can definitely use null there the next concept is before no safety our variables could actually be two things it could either be the actual variable let's pretend we have a string it could either be a string or they could be no with null safety if you're now defining something as a string it can only be that string but we get another option of a different type of variable a variable that we purposely define as being able to be a variable for example string or null this makes us conscious of the type of coder writing and now if you need something to specifically have a null case we must define it ourselves and with those type of variables the dark compiler has built in null safety or null protection so basically if you have a variable that can be string or null and you're trying to print it without maybe defining it we get an error before we even compile those should explain sort of why we need null safety and how no safety works and there's one more specific topic i want to cover and that's sound null safety remember how i said there's two different types of variables there's one that will for sure be that type of variable and another one that could be that type of variable or null so with sound null safety we can guarantee that 100 of the time that string variable if we don't define it to be null safe it will always be variable now you think that'd be the norm but there's actually only one other language that does that right now and that's swift and the benefit that gives us is it makes our code faster the compiler doesn't have to do null checks on stuff that we have not specifically defined that it can't have the option of no so sound null safety guarantees us 100 that the variable will either be nullable or non-nullable and there's nothing in between so now there's a couple keywords and new operands that we can use to work with null safety and to make our code a little bit easier there's four of them total let's go through them one by one the question mark we've had the question mark before in our apps but now if you put it right after the variable name it will tell us whether it is nullable or non-nullable so the not knowable which means it will 100 be a string this one which is what we will probably be using most of the time just like we are now but then if we do want it to have the null case we put a question mark right after the string and now we have a nullable string so if we have a name constructor with a value that we actually need so a value of this type which is not nullable you will need to use the keyword required in the constructor to let whoever is creating that object to know that this stuff is required then we have the late keyword which is used for non-nullable types and this basically lets you know that we're going to define this later now with a nullable type that's no problem you're already allowed to define it later but with non-nullable types if we want to define it later but before it is used then we need to use the late keyword i'll show an example in the code it should be easier to understand and then the last one is the bang operator this pretty much tells the compiler to get away and let me handle this you're basically saying i know that this for sure will not be no at this point in the app so don't give me the error all right so here we have the basic code that you've probably used in your apps already we have a class called person with a string for their name and a string for their favorite sport now this looks like code that you wrote back in the day but the key thing here is these cannot be known but we change this basketball to no you'll see we get an error saying argument cannot be no since it is assigned to string now if we put this question mark right after the string we'll see that problem will go away this can be no we have no issues that's the key thing right there for null safety pretty much making sure your variables can either be null or they can't be known if they can't be whenever a case happens where your variable could be null the compiler is going to shoot out an error for you okay now remember the required keyword so if you use name constructor you'll see that you get an error here the error says the parameter name can't have a value of no because of its type since name parameters mean they are optional this is not really a legal way to write code you're saying this optional one can be stored into something that can't be known to fix that we add the required keyword that you saw now we're saying this entry is the required entry so that we can meet that condition that the name value variable will not be no we see since we our string is a noble type we can remove it we have no problems here and then if we don't have the name you will see an issue here pop-up that this name field is required all right so i just pasted in some code remember there was two more keywords the late keyword and then the bang operator just as a warning you should use these as little as possible they are overriding what the compiler provides for you and most of the time the compiler will probably be right but there are definitely cases where you might know more than a compiler and you just want to tell it to you know mind its own business this is a potential issue we can see for this function we want to pass in a string which is not nullable this needs to have something that's not null in here right here our string is notable and we're passing it here with a potential note we'll see the problem the argument no both string cannot be assigned to parameter type string that's pretty much saying this can be no and we do not want to pass that so how do you fix it the first way to handle this is just to do a null check before you even call the function now hopefully that's the type of stuff that you're already doing in your app the second way is something you might have already been doing if it is no then you can return some other string but now the last way you could actually do it is you overwrite it with an exclamation point this is saying at this point i certify that the current name will not be no and to just pass it now the code we're writing obviously means we made a mistake here you'll see we have a script error because at this point it is no a scenario you would use this in is let's say you have a firebase call right here and you're calling your firebase application and you know 100 sure that you will always have something in the current name field so you're certain that you can pass it with a bang operator now the late keyword comes in handy in a separate scenario for this example we're going to import the dart math library and here we're going to see a separate scenario so we have a string but it can't be no and we have a random function that executes sometimes and potentially will give us the return of the name tardis and we'll be okay but you'll notice our compiler is complaining that this non-nullable variable must be assigned before it's used in this case it won't always be a sign for sure again using the late keyword we can make that error go away and we're gonna for sure assign it later before we use it with this keyword you're telling the compiler just trust me i know what i'm doing this will be set later and don't worry about it but it still catches stuff like this in case there's nowhere that it can possibly assign it it'll still give you an error so it'll protect you a little bit but definitely less than it does normally now in case like this i'd say we call firebase here for some boolean type when we know it will always be true then we just tell the string that we'll assign it later for sure and to not worry about it the bang operator is used for nullable types to let them know that it will for sure not be noble at that point and the late keyword is for non-noble types letting the compiler know that i will for sure declare it at a later point there we go i hope that simple code helped explain it a little bit now dart actually provides a migration tool so if you have a complete app that you've already built and you're not looking forward to upgrading it to null safety first i recommend you definitely do because it'll make your code base a little nicer and safer but two there's also a migration tool that dart provides so it kind of does that automatically for you and then you have to just go check through to make sure it did it right i will link this article in the description so make sure to check that out and hopefully migrate your apps to be better as far as when is coming my bet is on the march 3rd event it should come to stable and then everybody will be using it and hopefully our apps will become a lot better so that's it for this video if you want a longer more in-depth video there's a link in the description so make sure to check that out if you want some more flutter videos from me there should be a couple cards popping up here but thank you for watching this one make sure like subscribe and share if you enjoyed it and see you next time
Info
Channel: Tadas Petra
Views: 10,848
Rating: 4.9479556 out of 5
Keywords: coder, coding, programming, coding fundamentals, programming fundamentals, cs university, flutter, dart, null safety, flutter null safety, sound null safety, null concepts, swift, ios, android, nullable, non-nullable, late, required, migration tool, flutter migration tool, dart migration tool, flutter march 3rd, flutter stable, flutter beta, flutter release
Id: _VrulBiOV_o
Channel Id: undefined
Length: 9min 2sec (542 seconds)
Published: Fri Feb 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.