Late Keyword in Dart

Late Keyword In Dart

In dart, late keyword is used to declare a variable or field that will be initialized at a later time. It is used to declare a non-nullable variable that is not initialized at the time of declaration.

Example 1: Late Keyword In Dart

In this example, name variable is declared as a late variable. The name variable is initialized in the main method.

// late variable
late String name;

void main() {
  // assigning value to late variable
  name = "John";
  print(name);
}

targets

Run Online

When you put late infront of a variable declearation, you tell Dart the following:

  • Don’t assign that variable a value yet.
  • You will assign value later.
  • You will make sure the variable has a value before you use it.
Info

Note: The late keyword is contract between you and Dart. You are telling Dart that you will assign a value to the variable before you use it. If you don’t assign a value to the variable before you use it, Dart will throw an error.

Example 2: Late Keyword In Dart

In this example, there is Person class with a name field. The name field is declared as a late variable.

class Person {
  // late variable
  late String name;

  void greet() {
    print("Hello $name");
  }
}

void main() {
  Person person = Person();
  // late variable is initialized here
  person.name = "John";
  person.greet();
}

targets

Show Output
Run Online

Usecase of Late Keyword In Dart

Dart late keyword has two use cases:

  • Declaring a non-nullable variable or field that is not initialized at the point of declaration.
  • Lazy initialization of a variable or field.

What Is Lazy Initialization

Lazy initialization is a design pattern that delays the creation of an object, the calculation of a value, or some other expensive process until the first time you need it.

Info

Note: Using late means dart doesn’t initialize value right away, it only initializes when you access it for the first time. This is also called lazy loading.

Example 3: Late Keyword In Dart

In this example, the provideCountry function is not called when the value variable is declared. The provideCountry function is called only when the value variable is used. Lazy initialization is used to avoid unnecessary computation.

// function
String provideCountry() {
  print("Function is called");
  return "USA";
}

void main() {
  print("Starting");
  // late variable
  late String value = provideCountry();
  print("End");
  print(value);
}

targets

Guess the output before clicking on the Show Output button. If you remove the late keyword from the value variable, the provideCountry function will be called when the value variable is declared.

Show Output
Run Online

Example 4: Late Keyword In Class

In this example, the heavyComputation function is called when the description variable is used. If you remove the late keyword from the description variable, the heavyComputation function will be called when the Person class is instantiated.

// Person class
class Person {
  final int age;
  final String name;
  late String description = heavyComputation();

// constructor
  Person(this.age, this.name) {
    print("Constructor is called");
  }
// method
  String heavyComputation() {
    print("heavyComputation is called");
    return "Heavy Computation";
  }
}

void main() {
  // object of Person class
  Person person = Person(10, "John");
  print(person.name);
  print(person.description); 
}

targets

Show Output
Run Online

Example 5: Late Keyword In Class

In this example, the _getFullName function is called when the fullName variable is used. The firstName and lastName variables are initialized when the fullName variable is used.

class Person {
  // declaring late variables
  late String fullName = _getFullName();
  late String firstName = fullName.split(" ").first;
  late String lastName = fullName.split(" ").last;

// method
  String _getFullName() {
    print("_getFullName is called");
    return "John Doe";
  }
}
// main method
void main() {
  print("Start");
  Person person = Person();
  print("First Name: ${person.firstName}");
  print("Last Name: ${person.lastName}");
  print("Full Name: ${person.fullName}");
  print("End");
}

targets

Show Output
Run Online

Info

Note: If you remove the late keyword from the fullName variable, the _getFullName function will be called when the Person class is instantiated.

Late Final Keyword In Dart

If you want to assign a value to a variable only once, you can use the late final keyword. This is useful when you want to initialize a variable only once.

Example 6: Late Final Keyword In Dart

In this example, there is class Student with a name field. The name field is declared as a late final variable. The name field is initialized in the Student constructor. The name field is assigned a value only once. If you try to assign a value to the name field again, you will get an error.

// Student class
class Student {
  // late final variable
  late final String name;

  // constructor
  Student(this.name);
}

void main() {
  // object of Student class
  Student student = Student("John");
  print(student.name);
  student.name = "Doe"; // Error
}

targets

Show Output
Run Online

Video

Watch our video on the late keyword in Dart.