Late initialization error field has not been initialized (solved)| Flutter | Dart

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
here we'll take a look at late initialization  error in dart or flusher this is a common error   i'll first go ahead and simulate this problem  so here i am i'm trying to add item to the cart   and then i'm going to place an order and here it  says that late initialization error and it told me   that there is a field or variable its name is user  model and of course i have this variable in my   app so i have to find it now it's easy to find if  you see the log the log will tell you in general   where this variable of course if you wrote to the  app you know where it is but you can also know it   from here like as you can see it's pointing to  user model so you click on this one so here is   the problem now here definitely it says that  this object we created an instance and the   instance would be initialized later so  that's why we have late now if you have   a variable that has a late keyword before it  and before you're going to go ahead and use it   you have to initialize the value for it you have  to give it a value late means you will give it   value later now of course here it's not crashing  actually but this is the variable it's telling you   that this variable you used somewhere else but  you didn't initialize it you just declare it   here we just declared it but we didn't give  it any value and if we look down further uh   here we'll see okay there's another line and  over here we see that user model now this is   the same variable user model this one this is a  getter of user model now i'm using in this class   and here user model i said is not is null okay but  of course it is null but when you take a variable   late you have to make sure that you have some  value for it before you're going to use it   now i'm going to use it over here and i'm  expecting a value null but the variable that is   late it can't be null okay now one of the way to  deal with this one that i remove this one okay now   over here let's see so it raises other problems  over here okay so must be initialized so here   it says that if it's not null then the  user model must be initialized okay   but i can't really initialize it here  for my app architecture so what to do   so most of the time in this kind of situation  you'd put a question mark here what you're telling   that this variable the value this variable value  could be null okay so with this question mark   now for this variable we also have to get use  the getter and getter should also have this   nullable property so this is called it could  be null nullable property now with this most probably would get rid of the problem but  it could create other problem right now because   we have a lot of property inside of  this one now here it's saying that   is potentially null okay because the value of  this variable or fields they would be null as   well so you need to also define them okay so let's  see where we are using this one so you need to   also mark them as null so right over here okay  it could be null okay and right over here so user   model so i'm using different places so you need  to tell them okay over here it could be null okay   but of course we are doing a check we if it's not  null then we'll go inside this okay so it has to   do with once again over here because you put null  level over here so wherever you use this variable   you have to put nullable this question mark  right next to it otherwise you will get error   now let's run it one more time and looks like  we still have problem now problem is over here   so now here over here i have this  user model once again this one so over here i'm using it for a widget  so i'll in this widget i'm whenever   i come here i know that it is not null  so just put a bang operator okay and the all the other places where you use this  variable which you saying that it could be null   from here so every property in this variable  you should tag them either as this one   okay or using this bang operator  and now go ahead and restart it   the problem should be solved okay now  we are initializing all right and it did   now if you come over here if i go over there  and if you do check out yes so everything else   went well okay so we didn't have the crush so  the idea is if your value would be initialized   later a variable or an object it's better  to use this null checker sorry this nullable   operator over here okay instead of using  late because that could be very dangerous   all right and all the other places related to your  variable you should have similar changes thank you
Info
Channel: dbestech
Views: 32,185
Rating: undefined out of 5
Keywords: Late initialization error Field has not been initialized flutter, late initialization error field has not been initialized, lateinitializationerror flutter, LateInitializationError: Field, LateInitializationError: Field '_userData@32329253' has not been initialized, dart late initialization, dart late initialization error, dart error
Id: 9b79Rmqp-Ek
Channel Id: undefined
Length: 6min 2sec (362 seconds)
Published: Fri Apr 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.