DEV Community

Cover image for Hey! Dart variables - Dart programming - Part 3
Hoang Nguyen
Hoang Nguyen

Posted on

Hey! Dart variables - Dart programming - Part 3

Hello guys. So we had went through two article for introduce about Dart languages with me. In this article, I'll talk about Variables in Dart. Before start, I'd like to introduce you guys to using DartPad that can run dart code online without setup environment or using IDE. It's help me save lots of effort when want to test some new function. Now, FlutterWithMe!

First question. What's variables ?
Start with little example

String myName = "Hoang";
Enter fullscreen mode Exit fullscreen mode

Let me explain about this example. This mean we create a variable with type is String, variable name is "myName" and value of it is "Hoang". So basicly, variables store referrence. That's how you define a variable. But have one way to do that.

var myName = 2;
Enter fullscreen mode Exit fullscreen mode

If i create variable like that. It's mean i create "myName" variable without declare type first, and it'll get type when value is initialized. In this example variable "myName" have value is 2 and type is int.This one called implicit variable. I'll explain it now for easily understand.

  • Explicit variable: type of the variable is declared before or when the variable is set.
  • Implicit variable: type of the variable is assumed by the operators, but any data can be put in it.
String name = 2; //Explicit variable
var name = 2; //Implicit variable

With this example, if you define variable like first way, it'll get error warning because "2 is type int" can't set to String type. But with second way, "name variable" don't have specific type, so it's understand that it type is int when set value is 2.

Dart already introduce null-safety on Dart 2.0, it's help lots of programmer avoid NullPointException. So we'll talk about it now.

//Define variable can be null
int? phoneNumber; 

//Get error that you need initialize value first
int phoneNumber; 

//Define variable that can't be null
int phoneNumber = 0939; 
Enter fullscreen mode Exit fullscreen mode

If you define varible like this, that's mean "phoneNumber" variable have two type "int and null". And with this one you haven't initialize value first when define variable. But dart will always ask you to check if "phoneNumber is null or not", so remember that.

One way to create variable without set value first is using **"late" **keyword. Dart 2.12 added late modifier, which has two use cases.

  • Declaring a non-nullable variable that’s initialized after its declaration.
  • Lazily initializing a variable.
late phoneNumber;

phoneNumber = 0928;
Enter fullscreen mode Exit fullscreen mode

With this way, you don't need set value for phoneNumber when create variable first. But if you don't have value on variable but still use it, a runtime error occurs when the variable is used.

Image description

These are Build-in types that dart supported. But we usually using String, number, Booleans, Lists, Sets, Maps and null.But in dart have more specific type roles like:

Image description

The one difference with java is Dart don't have array. In dart array is List Object, and we usually called them lists (or collections).
Here's example list:

var list = [1,3,7,2,3];

// or define excatly type
List<String> listName = ["A","B","C"];
Enter fullscreen mode Exit fullscreen mode

One thing i want to talk about is Maps and Sets. The difference here is Sets is collection of unique objects. And Sets only have one parameter value not like Map is an object that associates keys and values.

// This is set define
Set newSet = {"one","two","three","one"};

// This is map define
Map newMap = {"key1":"one","key2":"two","key3":"three"};
Enter fullscreen mode Exit fullscreen mode

I think short introduce about variables is enough. I'll have more article to talk about collections ( which i see it's very cool ) in the future. If you like ( or not please try to like it ) can like and subcribe to help me have motivation to write more series. In next article i'll move to Medium and still archive into dev.to blog for new post. But you guy can help me with one follow in Medium. Appriciate too much and thanks guys for reading.

Top comments (0)