Dart Programming Language Basics

Melike Balpınar Güllü
6 min readJul 15, 2022

--

What is Dart?

Dart is an object-oriented programming language developed by Google. Whilst technically not restricted, it’s primarily used for creating frontend user interfaces for the web using the Angular-Dart or Flutter for Web and for the mobile apps using the Flutter.

Dart is under active development, compiled to native machine code (when used for building mobile apps).

Its inspired by modern features of other programming languages (mainly Java, JavaScript, C#) and strongly typed.

Dart is a compiled language (like C++,C,Go), that means, that our code isn’t executed like we write it, but instead, a compiler parses(also called binary, executable..) transform it to machine code.

What are the Types of the Dart?

Not all the programming languages are typed strongly (for example Java, Ruby, Python are strong unlikely Javascript, C++, C, PhP ect are weakly typed).

Every value we use in our program (e.g. some user input we’re saving) has a type — it could be “text”. For example in Dart and in pretty much all other programming languages, that would not be called “text” though but “string”. Here it is the some other types we should keep in mind:

Its also important to keep in mind that EVERY value in Dart is an object. More on that can be found below ( “What does “Object-oriented” mean?”).

Variables and Functions

Each programs, we typically need to store some values(datas). Not necessarily in a database or in a file but in memory. We might need to store some intermediate result, the input of a user before we process it or some information about our Flutter widget (e.g. “Should it currently be displayed on the screen?”).

Example:

is a variable that stores an integer (int) of value 30. The var keyword tells Dart that myAge is a variable. Alternatively to var, we could also use the type name — in addition to informing Dart about the variable, we would then also “inform Dart” about the type of data stored in the variable:

However, Dart has a feature called “type inference”. This means, that Dart is pretty smart about inferring types of values. If we created the variable with var, Dart is still able to infer that myAge is of type int(integer) because we initialize the variable (i.e. we assign a value right from the start) with an integer value. Because of that built-in type inference, it’s considered a good practice to not explicitly define the type but instead use var when defining variables but this snipped would not preferred.

That changes if we create a variable without an initial value — then, we should inform Dart about which type of data we plan on saving in there:

Besides variables, another core feature of any programming language are “functions”. Functions are allow us to “outsource” code into “re-usable code snippets”.

Here it is the some logic of adding two numbers in two o different places of our code. Instead of repeating ourselves, it would be great to put that logic into a function that we can call whenever as often as we want. Here’s the changed snippet, using a function:

Functions can take arguments (the data between the ( )) which are basically variables, scoped to the function. “Scoping” means that we can use the variables only inside of the block statement (= function body, the code between the { }) where we defined them. In addition, functions can also return values — like the result of the addition in this example.

What is object-oriented programming language mean?

Dart is an object-oriented programming language, and it supports all the concepts of object-oriented programming such as classes, object, inheritance, polymorphism, interfaces and abstract classes.

The main idea of object-oriented programming language is to reduce programming complexity and do several tasks simultaneously and hiding data from the user of the program at focus on real-life entities.

Object is a real-life entity such as a table, human, car, name, age etc. The object-oriented programming offers to identify the state and behavior of the object.

Objects are created with the help of “Classes” because every object needs a blueprint (the class) based on which we can then create (“instantiate”) it. Here’s an example class definition:

In this example we define a Person class which has two variables (which is called properties) . Inside of the greet method we can access the class properties which are name and age. We used toString method for the convert age(integer value) to string for the name we do not need to convert that because it is already is string format.

Additionally main function, it is special function for all programming languages, its execute first when the program is starts.

Syntax of the classes:

Here it is example for the class with setter, getter and constructor:

In this example we used encapsulation it is grouping or wrapping up of data and function to perform actions on data to single unit(class, field.

Getters and setters (accessors) are used to protect our data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value.

Constructor is a special function of the class that is responsible for initializing the variables of the class. Dart defines a constructor with the same name as that of the class. A constructor is a function and hence can be parameterized. However, unlike a function, constructors cannot have a return type.

Inheritance also supported by the Dart. Which is used to create new (child, subclass)class from the created(existing, parent) class. Dart provides extends keyword to inherit the properties of parent(base) class in child(derivate) class. In inheritance if we change something its also effects to other classes.

Polymorphism is concept where one thing has many forms. It can be two types; run time and compile time.

Run-time: can take different form while program is running.

Compile-time: can take different form while compilation. Its achieved by function overloading and operation.

Overloading method, there is multiple function with in same name but different parameters then these functions are said to be overloaded. But function overloading is not supported in dart at all.

--

--