dynamic
typeIn Dart, when a variable is declared as a dynamic
type, it can store any value, such as int
and float
.
The value of a dynamic variable can change over time within the program.
dynamic variable_name
The following code shows how to implement the dynamic
type in Dart.
// dynamic typevoid main(){// declaring and assigning value to variable a// of type dynamicdynamic a = 40;print(a);// reassigning value to aa= "Dart";print(a);// reassigning value to aa= 10.4;print(a);}
Here is a line-by-line explanation of the above code:
main()
function.a
of dynamic
type and assign an integer
value to it.string
value to it.float
value to it.Note: Once a variable is declared as a
dynamic
type, its value can change over time within the program.