Variables

Anonymous contributor's avatar
Anonymous contributor
Published May 15, 2024
Contribute to Docs

A variable is a location in computer memory used to store references, usually for use in a program. Variables serve as symbolic names (identifiers) for values in the computer’s memory. Variables allow programmers to manipulate and process data dynamically within their programs.

Syntax

When declaring a variable in Dart, the type of variable goes first, followed by the name, and then the value:

type name = value;
  • type: It can be any data type like Int, String or can also take the value var, const.
  • value: This is what the variable name represents. It corresponds to the data type type.

Note: The default type ‘var’ is inferred to be a string.

If an object isn’t restricted to a single type, specify the Object type (or dynamic if necessary):

Object name = 'value';

The Dart Language enforces sound null safety, allows to set default values and constants, and declares the late variables.

Example

To display a constant name “Alex” one should write:

void main() {
const name = 'Alex';
print(name);
}

The output for the above code will be:

Alex

All contributors

Contribute to Docs