Dart Basic Overview

Dart Basic Overview

ยท

9 min read

Hello All,

Let's go through some of the basic concepts of the Dart such as data types, OOP concepts etc.

So let's start introductions by performing the programmers ritual of printing the hello word using the Dart.

print('Hello World!');

Dart Introduction

Dart is an Object-oriented programming language developed by the Google. mainly used for creating the frontend user interface for the web and mobile apps in the flutter.

It's a compiled to native machine code (It's under active development). Dart is inspired by Java, JavaScript, C# (In my opinion the Dart almost feel like JavaScript) and it's Strongly-Typed.

Meaning of the Strongly typed is every value used in the Dart has a Type (Means value should be integer or text )

Data Types

The different types of data types in the Dart are

  • Number - Used of the for the number data type. It can be used for both numbers with or without decimal values ex: number GPA = 8.05 (My GPA of my BE ๐Ÿ’)

    There are 2 types of the numbers in Dart

    • int - Integer the number without decimal places. ex: int age = 21; (Which is my age ๐Ÿ˜‰)
    • double - double are the numbers with decimal places. ex: double height = 6.0 (My height in feet)
  • String - Used to store the text values. You can use both single or double quotes to mention the text. ex: String name = 'Game Monk' or String name = "Game Monk".
  • Object - Every value in the Dart is an object even an integer.

Variables and Functions

As you know if we want to process some information first we have to store those values somewhere first there comes the variables to our help. Variables got the name and store the values in them. ( You can consider them similar to a container).

var age = 21;

As you can see in the above code the age is the variable with name age and store the value 21 in it.

Now let's talk about the key work var which defines the type of the variable. variable can be of any type like integer, text or can be an object. The benefit of using the keyword var is that you can store any type of data unlike another keyword such as int which can only hold numbers without decimal values. But with the var you have to initialise it with a value as you can see in the code I have initialised it with value 21.

int age;
age = 21;

Another way we can store the value is to create variable without an initial value. but if we won't do this we explicitly have to mention the data type to be stored ( Means can't use var for this)

Another main feature of any programming language is Functions.

The function is a block of code which can be reused as many times required with having to write the same code repeatedly.

num addNumber(num n1, num n2) {
    // Due to use of num we can use this function for both int and double values.
    return n1 + n2;
}

var total = addNumber(1, 3); 
print(total) // Prints the value 4 
double totalvalue = addNumber(1.1, 3.3); 
print(totalValue) // Prints the value 4.4

The code of block contains the function addNumber which return the value after adding the number which we passed while calling the functions.

So the addNumber is the name of the function which we are going to use to call the function.

Also follow the convention of using the camelcase during naming the functions(Which is followed normally ๐Ÿ˜Ž) the keyword num is the return type of the function which tells about the function so the user can either store or process the returned value. num n1 and num n2 are the arguments which are passed during the function call to the pass some values to do the required the processing of information inside the function. arguments can be of any type or can be in any number. Also, the variables defined inside the functions remain inside the function through the concept called Scoping.

OOP concept in the Dart

Do you remember that Dart is an object-oriented programming language - that means that every value in Dart is an object. Even a simple number.

So what is Object?

Object are data structures. the object can be from simple data type such as int or double to a complex structure as a list of objects.

The object is created using the help of Classes because every object needs a blueprint based on which you can then create ("instantiate") it.

class Persom {
    var name = 'Max';
    var age = 30;

    void greet() {
        print('Hi, I am '+ name + ' and I am ' + age.toString() + ' years old!');
    }
}

So we define a class using the keyword class. Class contains two class-level variables called properties and the class-level function called Method.

The class only serves as a blueprint though! On its own, it does not give you an object! Instead, you can now create objects based on this class:

class Person {
    var name = 'Max';
    var age = 30;

    void greet() {
    print('Hi, I am ' + name + ' and I am ' + age.toString() + ' years old!';
    } 
}
void main() {
    var myself = Person();
    print(myself.name); // use the . to access class properties & methods
}

the main function is a special function in Dart - it's the function which Dart will execute first when your app starts.

As you can we can use the name and age variables inside the greet functions easily without any issues. But to use those values outside the class we need to follow some procedures for that:

Inside the main function, we create new object bases on the class Person by using the Person(). This process is called as Instantiating the class, by that we create an instance of Person. so we can use those properties using the . as you can see in the example we can use the name variable using the instantiated variable myself.name that way we can get the value stored inside the variable name in the class Person.

Thank You

Ok let's wrap it up here I will explain more of the OOP concepts in future blogs. Also, Thanks if you are still reading the blogs thanks for your time ๐Ÿ™. As usual, If you find any mistakes please let me know through the comments so I can repair my mistakes ๐Ÿ˜….