What are variables?
In this post we’re going to learn about variables in Dart, variables are like tiny-boxes that keeps references to a value, and this tiny-box can have a name so we can identify and remember it pretty easily.
How do we create them in Dart?
In Dart, we can create variables in three ways:
Using the reserved keyword var
We can create a variable using the reserved keyword var before the name of the variable and the value. Here’s an example:
This is called Type inference. What it does is that the compiler can automatically deduce the type of the variable or an expression.
Declaring the type of the variable
We can also declare the type of the variable as follows:
Using the reserved keyword dynamic
The dynamic keyword allows us to declare a variable that the type can change at execution tyme and can be defined in this way:
Now is your turn
You can try these concepts in IDE like Intellij idea community which is free, all you need is to install the Dart Plugin. Or in some online editors like Dartpad.
Learn more
If you liked this post I’m actually writing more like these in a free ebook which is basically a basic course of Dart that can help you to later start with Flutter, that awesome framework for develop multiplatform apps. If you’re interested you can get it for free following this link.
6 comments on “Variables in Dart”