Open In App

Dart – Variables

Last Updated : 20 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A variable name is the name assigned to the memory location where the user stores the data and that data can be fetched when required with the help of the variable by calling its variable name. There are various types of variable that are used to store the data. The type which will be used to store data depends upon the type of data to be stored.

Syntax: To declare a variable: 

type variable_name;

Syntax: To declare multiple variables of the same type: 

type variable1_name, variable2_name, variable3_name, ....variableN_name;
Introduction-to-Dart

Types of Variables

Type of the variable can be among: 

  1. Static Variable
  2. Dynamic Variable
  3. Final or const

Conditions to Write Variable Name

Conditions to write variable names or identifiers are as follows: 

  1. Variable names or identifiers can’t be the keyword.
  2. Variable names or identifiers can contain alphabets and numbers.
  3. Variable names or identifiers can’t contain spaces and special characters, except the underscore(_) and the dollar($) sign.
  4. Variable names or identifiers can’t begin with a number. 

Note: Dart supports type-checking, it means that it checks whether the data type and the data that variable holds are specific to that data or not.

Example of Dart Variable

Example 1:

Printing default and assigned values in Dart of variables of different data types. 

Dart
void main()
{
    // Declaring and initialising a variable
    int gfg1 = 10;

    // Declaring another variable
    double gfg2 = 0.2; // must declare double a value or it
                       // will throw error
    bool gfg3 = false; // must declare boolean a value or it
                       // will throw error

    // Declaring multiple variable
    String gfg4 = "0", gfg5 = "Geeks for Geeks";

    // Printing values of all the variables
    print(gfg1); // Print 10
    print(gfg2); // Print default double value
    print(gfg3); // Print default string value
    print(gfg4); // Print default bool value
    print(gfg5); // Print Geeks for Geeks
}

Output: 

10
null
null
null
Geeks for Geeks

Keywords in Dart

Keywords are the set of reserved words which can’t be used as a variable name or identifier because they are standard identifiers whose function are predefined in Dart. 

Dart_Keywords

Dynamic Type Variable in Dart

This is a special variable initialised with keyword dynamic. The variable declared with this data type can store implicitly any value during running the program. It is quite similar to var datatype in Dart, but the difference between them is the moment you assign the data to variable with var keyword it is replaced with the appropriate data type. 

Syntax: 

dynamic variable_name;

Example 2:

Showing how datatype are dynamically change using dynamic keyword.

Dart
void main()
{
    // Assigning value to geek variable
    dynamic geek = "Geeks For Geeks";

    // Printing variable geek
    print(geek);

    // Reassigning the data to variable and printing it
    geek = 3.14157;
    print(geek);
}

Output:

Geeks For Geeks
3.14157

Note: If we use var instead of dynamic in the above code then it will show error.

Error compiling to JavaScript:
main.dart:9:10:
Error: A value of type 'double' can't be assigned to a variable of type 'String'.
  geek = 3.14157;
         ^
Error: Compilation failed.

Final And Const Keyword in Dart

These keywords are used to define constant variable in Dart i.e. once a variable is defined using these keyword then its value can’t be changed in the entire code. These keyword can be used with or without data type name.

1. Final

A final variable can only be set once and it is initialized when accessed.

Syntax for Final:

// Without datatype
final variable_name

// With datatype
final data_type  variable_name

Example of final keywords in a Dart

Below is the implementation of final keywords in Dart Program:

Dart
void main() {
  // Assigning value to geek1 variable without datatype
  final geek1 = "Geeks For Geeks";
  // Printing variable geek1
  print(geek1);
  
  // Assigning value to geek2 variable with datatype
  final String geek2 = "Geeks For Geeks Again!!";
  // Printing variable geek2
  print(geek2);
}

Output:

Geeks For Geeks
Geeks For Geeks Again!!

Now, if we try to reassign the geek1 variable in the above program, then:

Output:

Error compiling to JavaScript:
main.dart:8:3:
Error: Can't assign to the final variable 'geek1'.
  geek1 = "Geeks For Geeks Again!!";
  ^^^^^
Error: Compilation failed.

2. Const

A constant variable is a compile-time constant and its value must be known before the program runs.

Syntax for Const:

// Without datatype
const variable_name;

// With datatype
const data_type variable_name;

Example const Keywords in a Dart Program

Below is the implementation of const Keyword in Dart Program:

Dart
void main() {
  // Assigning value to geek1 variable without datatype
  const geek1 = "Geeks For Geeks";
  // Printing variable geek1
  print(geek1);
  
  // Assigning value to geek2 variable with datatype
  const geek2 = "Geeks For Geeks Again!!";
  // Printing variable geek2
  print(geek2);
}

Output:

Geeks For Geeks
Geeks For Geeks Again!!

Now, if we try to reassign the geek1 variable in the above program, then output is as follows: 

Error compiling to JavaScript:
main.dart:8:2:
Error: Can't assign to the const variable 'geek1'.
 geek1 = "Geeks For Geeks Again!!";
 ^^^^^
Error: Compilation failed.

Null Safety in Dart

In Dart, by default a variable can’t be assigned Null value till it is defined that the variable can store Null value in it. This to avoid cases where user assign null value in Dart.

Example:

void main() {
   int a=10;
   a=NULL;
   print(a);
}

The above Program will return a error.

How to assign null value to variable in Dart?

To declare a variable as nullable, you append a ‘?' to the type of the variable. The declared variable will by default store null as value and even after assigning it values of your choice you can declare it as null afterwards.

Below is the implementation to assign null value to variables:

Dart
void main() {
   int? a;
   a=null;
   print(a);
}

Output:

null




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads