Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0


Variables in Dart Programming

variables in dart

Upon creating a variable in Dart, the Dart compiler allotted some space for that particular variable.

The Following are keywords used for declaring variables in Dart:

  • var
  • string
  • int
  • double
  • dynamic
  • final
  • const

1. Create a Variable using var keyword

Developers can use the var keyword for assigning variables in Dart. The Dart compiler will infer the type of data assigned based on the value given by the var keyword. The Nature of the var keyword is that once a data type is allocated during initialization to a var variable, it can be modified later. This is because the type is inferred by the Dart system. 

2. Create a Variable using String

The string keyword is utilized for assigning a string data type to a variable. The string represents a chain of individual characters. It is practical to declare the variable as a string and then assign a certain value accordingly. Further, reassigning the variable to a different value is also permitted.

3. Create a Variable using int

Variables declared using the int keyword represent a non-decimal number which can be either positive or negative numbers. It is feasible to declare a variable as an int and assign the respective value to it. Further, reallocation of the same variable is possible. It is important to note that a decimal value cannot be assigned to an integer variable.

4. Create a Variable using Dynamic keyword

Variable declared using dynamic keywords do not have any predefined data type.
Note that assigned values for a dynamic variable can be modified or altered at any juncture.

5. Create a Variable using Final keyword

The final keyword is utilized for the creation of immutable objects in Dart. The variable with the final keyword is utilized when the user wants to always store the same value and does not want it to change. An important fact to keep in mind is that the value assigned to a final variable cannot be modified once it is assigned.

6. Create a Variable using const keyword

The const keyword is utilized to let the user know that the value stored in the variable is constant and cannot change during program execution. If the value of the said variable is known at compile-time, then 'const' can be used over 'final'. The only difference between the final and const keywords is that the final is a runtime constant, which in turn means that its value can be assigned at runtime instead of compile time. It is important to remember that the value assigned to a const variable cannot be altered or modified once designated and doing so will produce an error.

Dart Variables variables in dart create variable dart programming Dart keywords declare variables