Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need to add late modifier while declaring the variables?

Its a general question. Not coding related. Everywhere I follow flutter tutorials I see the simple variable declarations. But when I write the same code (I think because of updating) it requires to add late modifier. late modifier can be used while declaring a non-nullable variable that's initialized after its declaration. Declaration of variables that will be initialize later is done using late modifier. That's what I read on the google. Why it is necessary to declare non-nullable variables. Vscode always underlines the variable showing error. But ate modifier. What do we need to change when we change variables to late. Cause its making following tutorials very difficult. set state is not helping.

like image 902
doc Avatar asked Mar 01 '23 11:03

doc


2 Answers

This is because of null safety in Dart. There are a lot of guides out there written before null safety was introduced. It is ok to declare a class member as nullable with the ?, so this is valid, and means that i can have the value null, therefore it is allowed without initialising its value:

int? i;

If you don't use the ?, you can still declare a member, but you have to assign a value to it, this is also valid:

int i=1;

But this will be invalid, since you say that i can't be null, and you do not assign a value to it:

int i;

And here comes the late keyword. By using this you "promise" Dart, that you will initialise this value later, for example in an initState method like this:

class MyWidget extends StatefulWidget {
  const MyProfile({Key? key}) : super(key: key);
  @override
  _MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
  late int _i;

  @override
  void initState() {
    super.initState();
    _i = 1;
  }

You have to keep this "promise", otherwise you will get a runtime error.

Another option is that you create a constructor for the class that has required members, it will ensure that these values can't miss in any instance created, so Dart is ok with it:

class MyClass {
    int i;
    MyClass({required this.i});
}

In this last example, if you omit required keyword, you will get an error, because i is not nullable, and if it is not required by the constructor, it could be null. Still, it will work without required, if you make i nullable by declaring it like int? i;.

like image 142
Peter Koltai Avatar answered Mar 04 '23 10:03

Peter Koltai


the late modifier is part of the new null-safety by dart it's used to tell the compiler to treat this variable as non-nullable and will be initialized later without it the compiler will treat the variable as nullable and give error

late String name;

@override
  void initState() {
   
    super.initState();
    name = "example";//the compiler will not see this to know that you have initialized the variable
  }
  @override
  Widget build(BuildContext context) {

    if(name=="example"){

      ...
    }
  }
like image 38
Omar Mahmoud Avatar answered Mar 04 '23 08:03

Omar Mahmoud