DEV Community

Cover image for Variables in Dart
Giuseppe Vetri for Codingpizza

Posted on • Updated on • Originally published at codingpizza.com

Variables in Dart

Variables - EN

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:

var year = 1990;
print(year)
//The result is going to be: 1990
Enter fullscreen mode Exit fullscreen mode

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:

String name = "John";
print(name)
//The result is going to be: John
Enter fullscreen mode Exit fullscreen mode




Using the reserved keyword dynamic

The dynamic keyword allows us to declare a variable that the type can change at execution type and can be defined in this way:

dynamic changeType = 1234
The type can change at runtime
changeType = "I'm a string now"
//The result is going to be: I'm a string now
Enter fullscreen mode Exit fullscreen mode




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 a basic course of Dart that can help you to start with Flutter, that awesome framework for develop multiplatform apps. If you're interested you can get it for free following this link..

Top comments (0)