Dart Tutorials: DataTypes in Dart

In this post, we will discuss some initial key concepts of Dart that are absolutely necessary for beginners. First, like Python, Dart is an object-oriented programming language. Everything is an object here.

You can try out all the examples using DARTPAD

Data Types

Data types are types of data that we can represent in a programming language, such as an integer is a nonfractional numerical value like 1, 2, and so on. Later when needed, we can also manipulate these values in our program.

For example, in a calculator we do lots of numerical operations such as additions, subtractions, etc. The default value of most data types is null. So, we need to mention what data type we are going to use.

Dart is statically typed, meaning that each variable in Dart has a type that must be known when you compile the code. The variable type cannot change when you run the program. C, Java, Swift and Kotlin are also statically typed.

This contrasts with languages like Python and JavaScript, which are dynamically typed. That means variables can hold different kinds of data when you run the program. You don’t need to know the type when you compile the code.

We use variables to reference those types that are actually stored in memory. Consider the following:

	int a = 1;

This means we first store the type of integer value 1 in our memory, and then we assign that value to the variable a In normal circumstances, in Dart, we mention what type we are going to use. If we use integers and strings, we write it like this:

int myAge = 12;
String myName = "John Smith";

In the previous examples, we have explicitly declared the type that should be used. In the next example, we do the same thing, but implicitly. Therefore, you can also write the same code in this way:

var myAge = 12;
var myName = "John Smith";

Built-in Types in Dart

The Dart language has special support for the following types, and you can always follow the strongly typed or duck typed pattern to initialize them:

  • Numbers 
  • Strings 
  • Booleans 
  • Lists (also known as arrays)
  • Sets 
  • Maps 
  • Runes (for expressing Unicode characters in a string) 
  • Symbols 

 Numbers(Int or double)

int and double types are subtypes of num. The num type includes basic operators such as +, -, /, and *; and they represent plus, minus, division, and multiplication, respectively. You can call them arithmetic operators.

In the below example we are going to see how to declare a num type and convert to string type

main() {
  int myNUmber = 542;
  double myDouble = 3.42;
  String numberToString = myNUmber.toString(); //allows you to convert int to string
  String doubleToString = myDouble.toString(); //allows you to convert double to string
  if ((numberToString == '542' && myNUmber.isEven) && (doubleToString == '3.42' && myDouble.isFinite)){
    print("Both have been converted from an even number ${myNUmber} and a finite double ${myDouble} to string. ");
  } else print("Number and double have not been converted to string.");

Understanding Strings

A Dart string is a sequence of UTF-16 code units. For absolute beginners, I’ll briefly describe UTF-8, UTF-16, and UTF-32. They all store Unicode but use different bytes.

Unicode support makes Dart more powerful, and you can create your mobile and web applications in any language. Let’s see one example where I have tried some Bengali script.

main(List<String> arguments) {
  String bengaliString = "বাংলা লেখা";
  String englishString = "This is some English text.";
  print("Here is some Bengali script - ${bengaliString} and some English script ${englishString}");

While handling strings, you should remember a few things. You can use both single quotes (“) and double quotes ("").

main(List<String> arguments) {
  String stringWithSingleQuote = 'I\'m a single quote';
  String stringWithDoubleQuote = "I'm a double quote.";
  print("Using delimiter in single quote - ${stringWithSingleQuote} and using delimiter in double quote - ${stringWithDoubleQuote}");

Booleans : To Be True or to Be False

You have already seen that Dart has a type called bool. The Boolean literals true and false have the type bool. They are compile-time constants.

Introduction to Collections: Arrays Are Lists in Dart

An array, or an ordered group of objects, is the most common collection in every programming language. In Dart, arrays are List objects . We will call them lists in our future discussions.

main(List<String> arguments) {
  List fruitCollection = ['Mango', 'Apple', 'Jack fruit'];

Introduction to Set 

In Dart, a Set is an unordered collection of unique items. There are small differences in syntax between List and Set. Let’s look at an example first to know more about the differences.

main(List<String> arguments) {
  var fruitCollection = {'Mango', 'Apple', 'Jack fruit'};

The Dynamic Keyword

 If you use the dynamic keyword instead of var, you get what’s effectively a dynamically typed variable:

dynamic numberOfKittens;

Here, you can set numberOfKittens to a String using quotes. You’ll learn more about the String type later in the tutorial.

numberOfKittens = 'There are no kittens!';

print(numberOfKittens); // There are no kittens!

numberOfKittens has a type, since Dart has static typing. But that type is dynamic, which means you can assign other values with other types to it. So you can assign an int value below your print statement.

numberOfKittens = 0;

print(numberOfKittens); // 0

Or, if you have a kitten in Schrödinger’s box, you could assign a double value:

numberOfKittens = 0.5;

print(numberOfKittens); // 0.5

We covered most of the topics in dart related to variables and datatype.You can try Out all the examples using the online console dartpad.

Hey I'm Venkat
Developer, Blogger, Thinker and Data scientist. nintyzeros [at] gmail.com I love the Data and Problem - An Indian Lives in US .If you have any question do reach me out via below social media