Flutter Tutorial - Migrate App To Flutter Null Safety

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
with the stable flutter version 2.0 flutter supports null safety and this means that you don't need to care anymore about any null exceptions which could occur because the flutter tooling automatically checks null safety and therefore we want to learn how to migrate existing flutter projects automatically so that they support now safety which makes development also more reliable and secure for you as a developer if you're new here subscribe to my channel and make sure to watch this video till the end let's get started by migrating an existing flutter project and therefore i take here the to-do app which i have developed in previous tutorials and therefore we will follow three simple steps to migrate our project first of all we check if the project is migratable because not every project you can migrate at this time secondly we need to update our dependencies and thirdly we can automatically migrate our coach before we start with the migration we want to do two other steps so first of all make sure that you upgrade your flutter system and then make sure that you have here at least the flutter version 2.0 and secondly make sure that you have a backup of your current flutter project therefore i added here to the git system and secondly i also commit the project so that we always can later go back as a backup if something goes wrong with these preconditions fulfilled we can start by checking if our project is migratable and therefore we go first of all to the location of our flutter project so i'm here within the root folder of this flutter project within my console and now we want to check if our flutter project is migratable therefore you write here this command and then we need to go to the resolvable column and here you need to make sure that each of these dependencies has here a check otherwise if not all of the dependencies have here a check then you cannot migrate to now saved yet in my case we can migrate to now safety because every dependency which i rely on for the to do app is supporting null safety now that we know that we can migrate our project we go to the pub spec jumble file within our flutter project and then we go back to the terminal and we copy here all the dependency version of each dependency and here i will put it right now to the cloud firestore and here i put then the new dependency inside and we do this with all the other ones so for example i also take this one flutter slideable and then i also put this dependency version here inside after that you have updated all the dependencies within your pubspec journal file we need to run pubget or you can also go to your ide and here you run get packages and with this we have finished the second step and we also need to do the last step to migrate our code automatically but before this you need to make sure that you have the right dart version so make sure that you have at least the dart version 2.12 if you don't have at least the start version then you need to go to dot dev slash get dot and then you can scroll here to the installing the dart sdk section and then you choose here your operating system so in my case mac and then you need to find here this command for upgrading your dart and then you copy here your command for your operating system and go back to your terminal or command line and then you paste it here inside and after this make sure that he has at least the dart version 2.12 installed and with this precondition we can run the command dot migrate which will then do the last step to migrate our code base automatically to nice safety and after dot has analyzed the project you also see here this url which you need to copy and then you can paste this url inside of your chrome browser for example and this leads then to this interactive ui where you can guide the migration process on the left side you see here all the files and you can mark the files which should be converted to not safety so if you deselect here for example this folder then this is not converted to null safety in my case i want to have you everything selected and after this you can go here through all these files and you can see what will be changed and this is every time here within this blue color which will be added and if something is removed then it is here in the red color and we want to go for example to this to do dart and here you see that he has added here this question mark he has removed here this add symbol and here at the bottom he has the question mark and an exclamation mark before we go here more closely into this file i also want to show you quickly what the differences between all these question mark and exclamation marks are the question mark is used to mark a type as a nullable type and this means in this case the datetime object can be null or it can be of a type of date time if we would have here a field which has no question mark after the type so this type is without any other exclamation mark or whatever then this means that we have a non-nullable type so this date time can only be non-null which means you can only put a daytime object inside and not a null instance and lastly we could use the exclamation mark to force a nullable type to a not nullable type so this is always what you can put here inside if you know that the json for example is not null and if you know that it is not now then you can force it to a not nullable value and now that we have learned about the differences we can also apply them here inside so let's say here we have five fields and you see two of them are required and therefore these both are not nullable however the two link tries to give us the inaudible value for the created time and the title and i also can click here on the title and i also can add here this exclamation mark hint and now both of these fields cannot have a nullable value anymore however this is fine because we have you this required statement therefore we require that this has a real value with an hour to do you also see that we have for example this description and also this is done field and by default we have supplied here some values and therefore we also want to make sure that these values are not nullable therefore i click here simply on this question mark and then we say here that we add an exclamation mark hint and we also click here on the second question mark and also add here this exclamation mark hint inside after this you can also click on this rerun with changes button and now you see that the nullable field is disappeared and we have here a not nullable value and lastly we also want to go to this json field and in our case we know every time exactly that this can never be null and therefore i also make it here to a not nullable value after this you can rerun the changes and then also this exclamation mark is not needed anymore and now you see after i have rerun the changes the exclamation mark disappeared because it is not needed here at this location anymore let's also look here at another example so i go here to the edit to do page and here inside you see that these are both nullable values however this cannot be totally true because within the init state we put here a value for the title inside and we also put a value for the description inside and with this we know that the title and description always have a value which is not null and therefore we also have a null safety a keyword which is called late which means that we mark a variable as late initialization and this is typically used with an init state method where something is later initialized like in this case and therefore we want to make both of these fields as a late type you cannot do this here directly within this ide because you don't have the assert button for late however you can always go to your code and then you can go here within your code to the place which we were inside before where we have here these two fields and here inside you can paste this comment late inside before this type and then you save this file and go back to the browser and then you rerun from your sources and then you should see that he has integrated the late type for us and with this we have here a late initialization for the values title and description and after you are done with all the changes within this tool you click here on apply migration and then you click on ok and after this you will update here the source code and you see also that he has put this late field for us inside and this means that we have a not nullable type instead of a nullable type what he suggested us before with this question mark and with this we have migrated our whole project to now safety and sometimes there could be some errors occurring within your project which you also need to fix then however in this case everything seems to work and we have successfully migrated to our safety hello everyone thank you so much for watching this video please make sure to give it a thumbs up and subscribe to my channel here to get the latest news about flutter and see you soon bye you
Info
Channel: Johannes Milke
Views: 16,553
Rating: undefined out of 5
Keywords: null safety basics, null safety in dart, how to use null safety in dart, null safety in dart tutorial, null safety, dart null safety, null safety tutorial, how to use null safety, intro to null safety, flutter, flutter null safety, flutter null safety migration, dart migrate, flutter null safety for beginners, flutter tutorial, flutter migration tool, dart migration tool, android null safety, ios null safety, null safe migration
Id: GLnQphHi1BY
Channel Id: undefined
Length: 9min 31sec (571 seconds)
Published: Thu Mar 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.