skip to Main Content

Before null-safety, we know that if we declare variable like this

int a;

Then a is pointing to a null object.

But, now with sound null safety, when we declare the non-nullable variable with late keyword like this,

late int b;

Then, what is the value in b ? or in other words, b is pointing to what ?

2

Answers


  1. The late keyword means that you are going to define new value as soon as possible, but you definitely will, so if you won’t set any value to and use it, it will through an exception like this:

    LateInitializationError: Field 'b' has not been initialized.
    

    So it does not mean that b is null when you use it, it means that although it is null now it will get value very soon before you use b.

    Login or Signup to reply.
  2. please check here
    late keyword is used initialise soon. so you need check that data is null or not. if it’s null means you should initialise or assign

    late int N;
    print(N ?? 123); // checking N is null if it null means by default  assign the 123. 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search