Dart Data Types and Variables Tutorial with Examples

0
9564
data types and variables in dart
How to declare variable and data type in dart

Now it is time to question what are Dart data types and variables. In this lesson we will cover the concept of dart data types and variables . We will learn how to declare variables in Dart and then we will focus on what are the various data types available in Dart.

Data Types in Dart

Dart provide us various built in data types in Dart.

  • Numbers
    • int
    • double

As contrast to Java or C++ Dart does not have anything like float or long. Dart offers just two types of number *Int and *Double.

  • Strings
  • Booleans
  • Lists
  • Maps
  • Runes
  • Symbols

In this tutorial we will go through only *Int *String and *Boolean data types. As we are just start learning we must go step by step.

Note: All the data types in dart are Objects. It means everything in Dart is objects and it inherits object properties. Which means default value of all these data types is null.

Dart Variable Declaration

We would like to clear the variable concept with the help of an example. Let’s suppose you want to declare a name may be your own name. The syntax is like Java

String fName = 'Waheed';

Here is the explanation.

  • String is the Data Type
  • fName is the Variable name
  • Waheed is the Value.

Dart has one more syntax to declare the same type of the value. In this case the syntax will be

var fName = 'Waheed';

The type of the fName variable is inferred as String. The compiler will check we have var keyword not String so type depends on value which in this case is String.

Let’s take another example. Suppose you want to declare a number as well. It could be your tax number with value 123456

var taxNo=123456;

We can declare the same value explicitly as int in Dart.

int taxNo=123456;

We don’t want to miss the boolean example. If a person is employed value will be true otherwise false. Let’s do this with a variable name isEmployed.

var isEmployed =true;

Or explicitly tell the compiler it is a boolean value.

bool isEmployed =true;

So far we have a good understanding with some of the basic variable and data types. Let’s clear the concepts with one dart variables and data types example code.

void main() {
  
  String fName = 'Waheed';
  var lName='Akhtar';  // It is inferred as String automatically 
  
  int taxNo=123456;
  var houseNo=405;    // It is inferred as int automatically 
 
  bool isEmployed =true;
  var isWorking = true; // It is inferred as String automatically 
  
  print(fName);
  print(lName);
  print(taxNo);
  print(houseNo);
  print(isEmployed);
  print(isWorking);
   
}

Here is the output of this.

Waheed
Akhtar
123456
405
true
true

In Dart, we can declare hex and exponent values too. Here are the hex and exponent value example.

void main() {
  
  int hexValue = 0xEAADEBC;
  double exponent=1.54e4;
  print(hexValue);
  print(exponent);
   
}

Here is the output of this code.

246079164
15400

I hope now you have a good understanding of Dart Data Types and Variables. It is time to learn about Literals and String Interpolation in Dart. Enable the notifications to learn about each and every concept of Dart language.

LEAVE A REPLY

Please enter your comment!
Please enter your name here