Dart Variables

Summary: in this tutorial, you’ll learn about Dart variables and how to use them to store and manage data effectively.

Introduction to Dart variables

In programming, you need to manage values like numbers, strings, and booleans. To store these values in programs, you use variables.

A variable is an identifier that stores a value of a specific type. By definition, a variable is associated with a type and has a name.

Declaring variables

The following syntax illustrates how to declare a variable:

type variableName;

In this syntax, the type specifies the values that the variable can store. Since Dart is a statically typed language, you need to explicitly specify the variable’s type.

The following shows some common types in Dart:

  • int – represents whole numbers like -1, 0, 1, 2.
  • double – represents practical values like 0.5, and 9.98.
  • String – represents text such as "Good Morning!".
  • bool – represents Boolean values including true and false.

By convention, the variable name use lowerCaseCamel. It means that you should capitalize the first letter of each word except the first word, and use no separators. Also, you should name the variables as descriptively as possible.

The following example declares a variable with the integer type:

void main() {
  int httpStatusCode;
}Code language: JavaScript (javascript)

Assigning a value to a variable

To assign a value to a variable, you use the assignment operator (=). For example, the following assigns 200 to the httpStatusCode variable:

void main() {
  int httpStatusCode;
  httpStatusCode = 200;
}Code language: JavaScript (javascript)

To make it shorter, you can both declare a variable and initialize its value using a single statement like this:

void main() {
  int httpStatusCode = 200;
}Code language: JavaScript (javascript)

Once you assign a value to a variable, you can use it. For example, the following displays the value of the httpStatusCode variable:

void main() {
  int httpStatusCode = 200;
  print('httpStatusCode: $httpStatusCode');
}Code language: JavaScript (javascript)

Output:

httpStatusCode: 200Code language: HTTP (http)

Note that if you use a variable that has not been initialized or assigned, you’ll get a compilation error.

Assigning a variable to another

Dart allows you to assign a variable to another:

variableName = anotherVariable;

For example:

void main() {
  int httpStatusCode = 200;
  int response = httpStatusCode;

  print('response: $response');
}Code language: JavaScript (javascript)

Output:

response: 200Code language: HTTP (http)

How it works.

First, declare the httpStatusCode variable and initialize its value to 200:

int httpStatusCode = 200;

Second, declare the response variable and assign the httpStatusCode to the response variable:

int response = httpStatusCode;

Third, display the value of the response variable:

print('response: $response');Code language: PHP (php)

Note that the response and httpStatusCode has the same value. However, if you change the value of httpStatusCode variable, it won’t affect the response variable. For example:

void main() {
  int httpStatusCode = 200;
  int response = httpStatusCode;
  print('httpStatusCode: $httpStatusCode,response: $response');

  httpStatusCode = 500;
  print('httpStatusCode: $httpStatusCode,response: $response');
}Code language: PHP (php)

Output:

httpStatusCode: 200,response: 200
httpStatusCode: 500,response: 200Code language: HTTP (http)

In this example:

First, assign the httpStatusCode to the response variable. Both variables hold the same value 200:

int httpStatusCode = 200;
int response = httpStatusCode;
print('httpStatusCode: $httpStatusCode,response: $response');Code language: PHP (php)

Second, assign 500 to the httpStatusCode and display the values of both variables:

httpStatusCode = 500;
print('httpStatusCode: $httpStatusCode,response: $response');Code language: PHP (php)

The output shows that changing the value of the response variable doesn’t affect the httpStatusCode variable.

If you have multiple variables with the same type, you can declare them all using a single statement. For example:

int httpStatusCode, response;

In this example, both httpStatusCode and response are integer variables. It uses a comma (,) to separate the variables.

Type inference

If you declare a variable and initialize its value at the same time as the following example, the Dart compiler will understand that 200 is an integer.

int httpStatusCode = 200;

Therefore, you don’t need to specify the type of the httpStatusCode variable explicitly. Instead, you can use the var keyword:

var httpStatusCode = 200;Code language: JavaScript (javascript)

In this case, the Dart compiler will infer the type of the httpStatusCode variable as an integer. This feature is called type inference in Dart.

Similarly, you can define a variable that holds a string without type inference like this:

String message = "OK";Code language: JavaScript (javascript)

Or you can use the type inference as follows:

var message = "OK";Code language: JavaScript (javascript)

The type inference also works if you assign a variable to another:

var message = "OK";
var response = message;Code language: JavaScript (javascript)

Summary

  • A variable is an identifier in the program that holds a value of a specific type. The value of the variables can be changed throughout the program.
Was this tutorial helpful ?