From the course: Flutter Essential Training: Build for Multiple Platforms

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Difference between var and dynamic

Difference between var and dynamic

- [Instructor] We have learned that when we define variables it is important to define the data type along with it as well. In other words, we explicitly declare the type that would be inferred. However, what if we do not know the type beforehand? That's when var or dynamic comes handy. In this case, we have redefined the data type as double but what if we want to keep things open? We can make this as either var or dynamic. So let's elaborate on this example. When we assign a value to a dynamic declaration of a variable the compiler assigns the data type, depending on the value assigned. In this case, it is a double value. So the compiler will assign double to price. You can verify this with this runtimeType property. See, this is a double. Now, if the same variable is reassigned a string value then what happens? So in this case, price becomes rupees 40.5. This is a string value. Now if you print(price.runtimeType)…

Contents