4. Dart Variables

4. Dart Variables

AI Reading

Quick summary of this article

Dart provides several ways to declare variables, each suited to different use cases. The `var` keyword lets the compiler infer the type, while explicit types like `String` or `int` give you more control. For values that should not change, you can use `final` (set once at runtime) or `const` (fixed at compile time). The `late` keyword allows you to declare a variable with a known type but initialize it later, and `dynamic` lets a variable hold any type that can change over time. Dart also includes null safety, meaning variables are non-nullable by default unless you explicitly mark them with a `?`.

  • Use `var` for type inference (e.g., `var name = 'Alice'` infers String) or specify the type directly (e.g., `String name = 'Bob'`).
  • `final` variables can only be assigned once (e.g., `final city = 'New York'`), while `const` variables must have values known at compile time (e.g., `const pi = 3.14`).
  • `late` is for variables you will initialize later but whose type is already known (e.g., `late String description`).
  • `dynamic` allows a variable to change type over time, such as from a String to an int to a bool.
  • Dart's null safety means variables cannot be null by default; use `?` to make them nullable (e.g., `int? age = null`).

                           Variables  in Dart

In Dart, variables can be declared in various ways depending on their intended use. Here’s a detailed explanation of different types of variable declarations with examples:

1. Using `var`

The `var` keyword is used to declare variables without explicitly specifying their type. The type is inferred by the Dart compiler:

void main() {
  var name = 'Alice'; // String type inferred
  var age = 30;       // int type inferred
  var isStudent = true; // bool type inferred
  
  print('Name: $name, Age: $age, Is Student: $isStudent');
}
  1. Using Explicit Types

 

You can explicitly specify the type of a variable:

void main() {
  String name = 'Bob';
  int age = 25;
  double height = 5.9;
  bool isEmployed = true;
  
  print('Name: $name, Age: $age, Height: $height, Is Employed: $isEmployed');
}
  1. Using `final`

The `final` keyword is used to declare a variable that can be set only once. The value cannot be changed after it is set:

void main() {
  final city = 'New York'; // Type inferred as String
  // city = 'Los Angeles'; // Error: Cannot change the value of a final variable
  
  final int population = 8000000;

  
  print('City: $city, Population: $population');
}
  1. Using `const`

The `const` keyword is used to declare compile-time constants. These variables are immutable and their values must be known at compile time:

void main() {
  const pi = 3.14; // Type inferred as double
  const String greeting = 'Hello, World!';
  
  print('PI: $pi, Greeting: $greeting');
}
  1. Late Initialization with ‘late’

The `late` keyword is used to declare a variable that will be initialized later, but the type is known:

void main() {
  late String description;
  
  description = 'Dart is a client-optimized language for fast apps on any platform.';
  
  print(description);
}
  1. Using Dynamic Type with `dynamic`

The `dynamic` keyword allows a variable to hold values of any type and the type can change over time:

void main() {
  dynamic variable = 'Hello';
  print(variable); // Output: Hello
  
  variable = 123;
  print(variable); // Output: 123
  
  variable = true;
  print(variable); // Output: true
}
  1. Null Safety

Dart has null safety, which helps avoid null pointer exceptions. 

By default, variables cannot be null unless explicitly declared as nullable using the `?` operator:

void main() {

  int? age = null; // Nullable integer
  print(age); // Output: null
  
  age = 30;
  print(age); // Output: 30

}

 

 Summary of Variable Declaration Syntax

  1. `var` for type inference:

      var name = ‘Alice’;

  1. Explicit type:

   `String name = ‘Bob’;

   “`

  1. `final` for single-assignment:

   final city = ‘New York’;

   “`

  1. `const` for compile-time constants:

   const pi = 3.14;

   “`

  1. `late` for late initialization:

   late String description;

   “`

  1. `dynamic` for dynamic types:

      dynamic variable = ‘Hello’;

   “`

  1. Nullable types:

    int? age = null;

These different ways of declaring variables allow for flexibility in how you define and use variables in your Dart programs.

 

Leave a Reply

Your email address will not be published. Required fields are marked *