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 type void main() { // declaring and assigning value to variable a // of type dynamic dynamic a = 40; print(a); // reassigning value to a a= "Dart"; print(a); // reassigning value to a a= 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.
RELATED TAGS
CONTRIBUTOR
View all Courses