loading

Dart Variables

In computer memory, a variable is used to store values and refer to memory locations. The Dart compiler sets aside memory when we create a variable. The kind of variable determines how big the memory block should be. A few guidelines should be followed when creating a variable. This is an illustration of how to create a variable and give it a value.

				
					var name = 'Devansh';  
				
			

The string value ‘Devansh’ is stored in a variable here named name. The variables in Dart hold references. A reference to a String with the value Devansh is stored in the aforementioned variable.

Rule to Create Variable

In any programming language, naming a variable correctly is a crucial responsibility. To define a variable, the Dart provides a set of rules. These guidelines are listed below.

  • Special characters like whitespace, mathematical symbols, runes, Unicode letters, and keywords cannot be present in the variable.
  • An alphabet ([A to Z], [a to z]) should be the variable’s initial character. It is not permitted to use digits as the initial character.
  • Variables are subject to case. For instance, AGE and variable age are handled differently.
  • Special characters like #, @, ^, &, and * are prohibited, with the exception of the underscore (_) and dollar symbol ($).
  • The software should be able to read and retable the variable name.

How to Declare Variable in Dart

Before utilizing a variable in a program, it must be declared. To declare a variable in Dart, use the var keyword. Since Dart is an infer type language, the compiler may automatically determine the type of data based on the values supplied to the variables. Below is the syntax.

Syntax

				
					var <variable_name>  = <value>;  
				
			

or

				
					var <variable_name>;  
				
			

Example

				
					var name = 'Andrew'  
				
			

The variable name in the example above has allotted some memory space. Utilizing the semicolon (;) is essential as it divides one program statement from another.

Type Annotations

The Dart language is an infer language, as we had previously mentioned, but it also offers a type annotation. The type of value that the variable can store is suggested when it is declared. To make sure the variable can store a certain data type, we prefix the variable’s name with the data type in the type annotation. Below is the syntax.

Syntax

				
					<type> <variable_name>;  
				
			

or

				
					<type> <name> = <expression>;  
				
			

Example

				
					int age;  
String msg = "Welcome to JavaTpoint";  
				
			

The numeric data in the example above will be stored in a variable called age that we have declared. The string type data was placed in a variable called msg.

Declaring the variable with Multiple Values

Multiple values of the same type can be declared for variables in Dart. All of this may be done in a single statement, with commas used to separate each value. Below is the syntax.

Syntax

				
					<type> <var1,var2....varN>; 
				
			

Example

				
					int i,j,k;  
				
			

The numeric data in the example above will be stored in a variable called age that we have declared. The string type data was placed in a variable called msg.

Default Value

When a variable is declared without its value initialized, the Dart compiler assigns the variable’s default value, which is Null. The null value is first assigned to even the numerical type variables. Let’s think about the next illustration.

				
					int count;  
assert(count == null);  
				
			

Final and const

Final and const are used when we don’t want to modify a variable in the future. It can be applied with a type or in lieu of var. When a final variable is a compile-time constant, it can only be set once. Below is an example of how to create a final variable.

Example

				
					final name = 'Ricky';                               // final variable without type annotation.  
final String msg = 'How are you?';     // final variable with type annotation.  
				
			

It will generate an error if we attempt to modify these settings.

				
					name = 'Roger';                                 // Error: Final variable can't be changed.  
				
			

Compile-time constants are made with the const. A compile-time constant, such as an integer, string literal, const variable, etc., can have a value declared for it.

				
					const a = 1000;  
				
			

Another way to generate a constant value that cannot be altered after it is created is with the const keyword.

				
					var f = const[];  
				
			

It will generate an error if we attempt to alter it.

				
					f = [12];    //Error, The const variable cannot be change  
				
			
Share this Doc

Dart Variables

Or copy link

Explore Topic