Dart Basic- Variables

Home /

Table of Contents

Variables declaration

Variables are an essential part of programming languages as they serve as containers for values that are used in a program. In Dart, the variable names are called identifiers, and there are certain naming rules for them. For instance, identifiers cannot be keywords, cannot contain spaces and special characters, except the underscore (_) and the dollar ($) sign, and cannot begin with a number.

To ensure type-checking, Dart provides the feature of prefixing the variable name with the data type. It means that a variable must be declared before it is used. If a variable is declared without a static type, it is implicitly declared as dynamic. Also, a variable can be declared using the dynamic keyword in place of the var keyword.

For example, consider the following code snippet:

void main() {
    dynamic x = "tom";
    print(x);
}


Here, we have declared a variable x and assigned a string value to it. Since we haven’t specified the data type of x, it is implicitly declared as dynamic. The output of this code will be tom.

In Dart, constants can be declared using the final and const keywords. These keywords are used to declare constants, and their values cannot be modified once they are declared. They can be used in conjunction with the variable’s data type or instead of the var keyword.

The const keyword is used to represent a compile-time constant. Variables declared using the const keyword are implicitly final. For example:

void main() {
    final val1 = 39;
    print(val1);
}


Here, we have declared a constant variable val1 and assigned a value to it. Since we haven’t specified the data type of val1, it is implicitly declared as final. The output of this code will be 39.

Here are some expanded examples of variable declarations and usage in Dart:

Basic Variable Declaration

Example:

void main() {
    String name = "John";
    int age = 30;
    double salary = 5000.50;
    bool isEmployed = true;
    print("Name: $name\nAge: $age\nSalary: $salary\nEmployed: $isEmployed");
}

Output:

Name: John
Age: 30
Salary: 5000.5
Employed: true

In this example, we declare four variables with their respective data types and assign values to them. We then print the values of these variables using string interpolation.

Dynamic Variable Declaration

Example:

void main() {
    dynamic x = "tom";
    print(x);
    x = 30;
    print(x);
}

Output:

tom
30


In this example, we declare a dynamic variable x and assign the string value “tom” to it. We then print the value of x. Later, we assign the integer value 30 to x and print it again.

Final and Const Variable Declaration

Example:

void main() {
    final int val1 = 39;
    const double val2 = 3.14;
    print("Value 1: $val1\nValue 2: $val2");
}

Output:

Value 1: 39
Value 2: 3.14

In this example, we declare two constants val1 and val2 with their respective data types. We then print the values of these constants using string interpolation. Since val1 is declared using the final keyword, its value cannot be modified at runtime. Similarly, since val2 is declared using the const keyword, its value is a compile-time constant and cannot be changed.

Variables play an important role in programming, and understanding their declaration and usage is essential for writing efficient and maintainable code. In addition to variables, Dart provides support for various data types, including numbers, strings, boolean, lists, and maps, to name a few. By using the right data types for the right purpose, developers can improve the performance and scalability of their Dart applications.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article