Blog
Dart dynamic variable
Dart dynamic variable
Let’s learn about the Dart dynamic variable in this tutorial. The var data type will replace the appropriate data type of the variable when a value is assigned to the variable.
Steps to create Dart new project in IntelliJ IDE:
https://www.testingdocs.com/new-dart-project-using-intellij-ide/
Dart dynamic variable
The Dart programming language supports a special variable type called dynamic data type. Dart provides the dynamic type, which allows a variable to hold values of any data type. The data type of a dynamic variable is resolved at runtime. When a variable is declared as dynamic, the variable can store any value during runtime without a specific data type.
Example
Let’s see how the dynamic keyword works. The datatype dynamically changes when we assign different data to the dynamic variable in the program.
/*
Dynamic variables Demo Program
Dart Tutorials - www.TestingDocs.com
*/
void main()
{
// Store a String
dynamic foo = "TestingDocs.com";
// print the value of the variable
print("Before : $foo");
// Let's reassign a number integer data type to the variable
foo = 2016;
// print the value
print("After : $foo");
}
The foo variable is the dynamic variable. The foo variable can dynamically store any data type during runtime.
We assigned a string data and later reassigned an integer number to the same variable.

Output
Before : TestingDocs.com
After : 2016
The difference between dynamic and var datatype in Dart is that the var will replace itself with the appropriate data type when assigning a value to the variable. If the foo variable is declared with the var keyword assigned a string data, and then later we cannot assign another data type to the variable.