Like in any programming language, we can assign different types of data into a variable.

Primarily we can divide the data into the following types

  • Numbers
  • Strings
  • Booleans
  • Lists
  • Enumerated
  • Maps
  • Objects

Numbers:

Dart have two types of numbers, one is integers and the other one is double.

We can create a variable to hold a number in four different ways in Dart.

int: A variable created with int keyword can hold a whole number

double: A variable created with double keyword can hold a floating point number

num: A variable created with num keyword can have an integer or a double number.

var: A variable created with var keyword and initialized with a number will have the type of the number assigned to it during the initialization.

example:

void main() {
  int myInt = 100;
  // myInt = 100.11; // not allowed, we get 'A value of type 'double' can't be assigned to a variable of type 'int'.' error

  double myDouble = 100.12;
  myDouble = 100; // allowed

  double myDouble2 = 200;
  myDouble2 = 200.22;

  var myVar = 100;
  // myVar = 100.11; // // not allowed, Dart automatically gives int type to myVar. So we get
  //'A value of type 'double' can't be assigned to a variable of type 'int'.' error

  var myVar2 = 100.12;
  myVar2 = 200; // allowed

  var myVar3 = 100.0;
  myVar3 = 200;
  myVar3 = 300.33;

  num myNum = 100;
  myNum = 100.234; // allowed
}

Pay attention to the way we created variables in the above example.

When we create a variable using int keyword, they we can re-assign only another integer into that variable.

In the earlier versions of Dart, to create a double variable, it was must to add a decimal point during the initialization. But this is not required now, as you can see in the above example that, we initialized the variable myDouble2 with a value of 200.

Since it is allowed to assign a number which looks like an integer (100 is same as 100.0) into a double, we can also re-assign a value that looks like an integer into a double variable later as we assigned 100 into myDouble in the above example.

This is not a problem if we use the keywords int or double. It can be a problem if we use the Dart’s preferred way of initialization, that is by using the var keyword.

If you want to initialize a variable with var keyword to hold a floating point number, initialize that variable with a decimal point, so that Dart can infer the appropriate data type to that variable like we did for the variable myVar3.

num type is like a generous number type which allows us to save either an integer or a double into that variable.

Though double type look like it can do what num type is doing, there is a difference between them.

Dart infers data type to variables initialized with num based on the current value of that variable, whereas if we initialize a variable with double, it will always be double.

example:

void main() {
  num myNum = 100.234;
  myNum = 100;

  double myDouble = 100.234;
  myDouble = 100;

  print(myNum.runtimeType);
  print(myDouble.runtimeType);
}

Output:

int
double

Like any other programming language, Dart also comes with several built-in methods to work with numbers such as converting an int to a double, string to int etc.,

Strings:

A String in Dart is nothing but a sequence of UTF-16 characters wrapped inside single or double quotes.

example:

void main() {
  String myString = "Hello World";
  String personName = 'John Doe';
}

Though we can use single or double quotes, Dart advises the developers to prefer single quotes. If you use double quotes for a string, then we get the following message from Dart.

“Only use double quotes for strings containing single quotes”

If the string itself contains a single quote (eg: John’s ), then we can use double quotes.

example:

void main() {
  String myString = "John's name";
}
String Interpolation

If you want to include a variable inside a string, traditionally developers used to use concatenation (normally by using ‘+’ operator) method to achieve the goal.

Dart allows the developers to include a variable inside a string by using $ notation ($ also converts a number into a string) .

example:

void main() {

  var id = 123;
  var johnInfo = 'Hello World, my id is: $id ';

  print(johnInfo);
}

Output:

Hello World, my id is: 123 

Instead of a normal variable, if you want to use a property of an object inside a string, we need to additionally add curly brackets ( {} ) to wrap the property.

example:

void main() {

  var john = Person();
  var johnInfo = 'Hello World, my id is: ${john.id} ';

  print(johnInfo);
}

class Person {
  final id = 123;
}
Multiline Strings

We can use triple quotes ( single or double, but single preferably ) in Dart to have multiline strings (this is similar to  “ in JavaScript which allows multiline strings ).

example:

void main() {

  var id = 123;
  var johnInfo = ''' 
        Hello World, 
        My Name : John 
        My Id: $id 
      ''';

  print(johnInfo);
}

Output:

Hello World, 
My Name : John 
My Id: 123

Long strings can be split into multiple lines line this

void main() {
  var id = 123;
  var johnInfo = 'Hello World,'
      ' My Name : John'
      ' My Id: $id';

  print(johnInfo);
}

Output:

Hello World, My Name : John My Id: 123

Here, please note that we did not use concatenation operator (+) at the end of the lines (you can use if you want to) , which we normally use in programming languages such as JavaScript.

Also note that, there are no line breaks in the output which we had with triple quotes.

Booleans:

To assign a boolean value (true of false) into a variable, we can use bool keyword or the plain var keyword. If we use var keyword Dart will infer the bool type to the variable if the assigned value is true or false.

example:

void main() {
  bool isLoggedIn = true;
  var isAdmin = false;

  print(isLoggedIn.runtimeType);
  print(isAdmin.runtimeType);
}

Output:

bool
bool

Instead of assigning a value directly into a variable like we did in the previous example, we can also assign the result of an expression like this

void main() {
  var emailID = 'user@example.com';
  var isAdmin = emailID == 'admin@example.com';

  print(isAdmin);
}

Output:

false

Enumerated:

Many programming languages including Dart support Enumerated data types which is shortly called as enums or enumerations.

Enums represents a specific number of constant values.

For example, suppose you are building an application in which a user can have three different roles, ‘Guest’ or ‘Registered’ or ‘Subscribed’ and you want to save it in variable userRole . While all the values that you want to allow are strings, if you make your userRole of type String, then the programming language allows any other string value inside that variable apart from the ones you only wants to allow, since it is just a string variable.

In this type of scenarios we can use enumerated data type which allows the developer fix the allowed values into a variable.

example:

enum Roles { Guest, Registered, Subscribed }

void main() {
  var userRole = Roles.Guest;
}

In the above example, we created an enum roles with the values of Guest, Registered and Subscribed (pay attention to the way we gave the values without quotes, though they are strings, we do not use quotes). After assigning a value into userRole, now the variable userRole cannot have any value other than the permitted 3 values from the enum.

Each item of the enum will also have an index property which gives the position of the item inside the enum. Similar to arrays, the index starts from 0.

example:

enum roles { Guest, Registered, Subscribed }

void main() {
  var userRole = roles.Registered;

  print(userRole.index);
}

Output:

1

Leave A Comment

Your email address will not be published. Required fields are marked *