DEV Community

Flutter Tanzania
Flutter Tanzania

Posted on

Understanding the Difference Between "const" and "late" in Dart

Dart is a modern, object-oriented programming language developed by Google that is widely used for building mobile, web, and desktop applications. When working with Dart, you may come across two keywords that can be used to declare variables: "const" and "late." While both keywords serve distinct purposes, it's essential to understand their differences to leverage their capabilities effectively. In this article, we will delve into the dissimilarities between "const" and "late" in Dart.

  1. Constant Variables with "const": The "const" keyword is used to declare variables whose values are known and won't change during runtime. When you declare a variable as "const," you are stating that its value is fixed and cannot be modified. The key characteristics of "const" variables are as follows:
  • Initialization: "const" variables must be initialized with a value at compile-time. This means that you cannot assign a value to a "const" variable that is determined dynamically during runtime.

  • Immutability: "const" variables are immutable, meaning their values cannot be changed once assigned. Any attempt to modify a "const" variable will result in a compilation error.

  • Compile-time Constant: "const" variables must be assigned a value that can be determined at compile-time. This includes literal values such as numbers and strings, as well as the result of const expressions involving other const variables.

  • Scope: "const" variables are known throughout their scope, and their values are resolved during compilation. They are generally used to declare values that are known in advance and won't change, such as mathematical constants or fixed configuration values.

Here's an example that demonstrates the usage of "const" in Dart:

const int numberOfDaysInWeek = 7;
const String welcomeMessage = "Hello, World!";
Enter fullscreen mode Exit fullscreen mode
  1. Late Initialization with "late": The "late" keyword, on the other hand, is used to declare variables that are initially uninitialized but will be assigned a value before their first use. The key characteristics of "late" variables are as follows:
  • Initialization Delay: Unlike "const" variables, "late" variables are allowed to be uninitialized when declared. This can be useful when you need to declare a variable but don't have an appropriate value at that moment. You are required to assign a value to the "late" variable before accessing it, or it will throw an exception.

  • Mutability: "late" variables can be reassigned multiple times after their initial assignment. This makes them suitable for scenarios where the value of a variable may change during runtime.

  • Nullable: By default, "late" variables are nullable. This means that they can hold a value of null until assigned a non-null value.

  • Scope: "late" variables are known throughout their scope and can be assigned a value at runtime. They are commonly used when the initialization of a variable is delayed due to asynchronous operations or when the value is determined at runtime based on some conditions.

Here's an example that demonstrates the usage of "late" in Dart:

late String username;

void main() {
  fetchUsernameFromServer().then((name) {
    username = name;
    print("Welcome, $username!");
  });
}

Future<String> fetchUsernameFromServer() async {
  // Simulating an asynchronous operation
  await Future.delayed(Duration(seconds: 2));
  return "JohnDoe";
}

Enter fullscreen mode Exit fullscreen mode

In the example above, the variable "username" is declared as "late" because its value is determined asynchronously by fetching it from a server. The actual value is assigned before its first use in the "then" callback.

To summarize, "const" is usedto declare variables with a fixed value that is known at compile-time and cannot be changed. On the other hand, "late" is used to declare variables that are initially uninitialized but will be assigned a value before their first use, allowing for delayed initialization and mutability.

Understanding the differences between "const" and "late" in Dart is crucial for writing efficient and error-free code. By leveraging the unique capabilities of each keyword, developers can ensure that their variables are appropriately declared and used according to their intended purpose.

Top comments (0)