Menu
Coddy logo textTech

Late Initialization

Part of the Fundamentals section of Coddy's Dart journey — lesson 81 of 94.

The late keyword allows you to declare non-nullable variables without initializing them immediately. The variable must be assigned a value before it's used.

Declare a late variable:

late String name;
// Use code before initializing

Initialize the late variable before using it:

name = 'Dart';
print(name);

After executing the above code, the output will be:

Dart
challenge icon

Challenge

Beginner

In this challenge, you'll practice using the late keyword in Dart. The late keyword allows you to declare a non-nullable variable without initializing it immediately, but you must assign a value before using it.

Complete the code by adding the late keyword to the playerName variable declaration. Then assign a value to it before it's used in the print statement.

Cheat sheet

The late keyword allows you to declare non-nullable variables without initializing them immediately. The variable must be assigned a value before it's used.

Declare a late variable:

late String name;

Initialize the late variable before using it:

name = 'Dart';
print(name);

Try it yourself

void main() {
  // TODO: Add the 'late' keyword to this variable declaration
  String playerName;
  
  // Game initialization code...
  print('Game starting...');
  
  // TODO: Assign a value to playerName before it's used
  
  // This will use the playerName variable
  print('Player $playerName has entered the game!');
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals