Dart Programming for Flutter

In this article I am giving basic introduction to Dart programming language which is used to develop mobile, web and server applications.

Tilak
4 min readJan 30, 2021

Dart is an open source programming language not only used for developing mobile applications, it is used in web, desktop, server applications and IOT devices. Dart is an object oriented programming language which is having syntax similar to C programming language.

Special Features

Dart is having four important features.

1. Object Oriented Programming : Dart is object oriented language in which it functions everything in the form of class and objects.

2. Statically Typed : Similar to C,C++and Java, Dart is also static typed language, which means the variables are need not to be defined before using.

3. C- Style Syntax : Even-though dart is object oriented programming language, its syntax is similar to C programming which helps the new programmers to study dart easily.

4. Multiple Runtime Environments : It means program written in Dart can be complied in multiple environments. It can be converted to Java script to run in browser, it can be converted to byte code in mobile applications.

Apart from these features, Dart has other features like constructors, constants, classes, interfaces, asynchronous programming etc.

Data Types

One of the most fundamental characteristics of a programming language is data types. The data types supported by Dart is listed below.

  1. Numbers — Numbers in dart is used to represent numeric literals. There are two types of Number data type, they are Integer and Double. Integer literals can be represented by int keyword and Double literals can be represented by double keyword
  2. Strings — Strings are used to represent string literals by using String keyword. String variable can be defined by using either double quotes or single quotes.
  3. Boolean — Boolean literals are represented by bool keyword and the value of Boolean variable will be either true or false.
  4. Maps and Lists — Dart doesn't support array concepts. So in order to represent collection of objects dart uses maps and lists data types. List data type in Dart is similar to the concept of an array in other programming languages. The Map data type represents a set of values as key-value pairs.

Functions

Functions are the building blocks of readable, maintainable and reusable code. Functions in Dart is similar to the functions in other languages such as Java , C etc. Functions can be defined with parameters and without parameters, with return type and without return type. The sample code below shows the usage of datatypes and functions in Dart.

main(){
String result;
int x, y, z;
result = add(x=10,y=9,z=3);
print('Sum of the digits is : $result');
}
String add(int x, int y, int z){
return (x+y+z).toString();
}

In the above sample code the Integer variable x, y, z are assigned to parameterized function called add() having return type String. The data returned by the add() function is assigned to result variable and displayed in the console using print function.

Use Dart pad for hands on experience.

Classes and Objects

Dart is an object oriented programming language which is associated with classes and objects along with its methods and attributes. A Class in terms of OOP is a blueprint for creating objects which encapsulates data for the object. Class is declared using keyword class. Definition of a Class starts from class keyword followed by class name and the body of the class is enclosed by a pair of curly braces.

Any entity that has a defined boundary such as state, Behavior and Identity is called Object. The period operator(.) is used in conjunction with the object to access data members from a Class. Below sample code shows the usage of class and objects in Dart.

Example: declaring class

class Book{
String bookName;
int edition;
printBookDetails(String name, int edition){
this.bookName = name;
this.edition = edition;
print('You are searching for a book named $bookName, Edition $edition');
}
}

Example: create object for class and access function

var obj = new Book();
obj.printBookDetails('Programming in Dart',2020);

Complete sample code:

void main(){
var obj = new Book();
obj.printBookDetails('Programming in Dart',2020);
}
class Book{
String bookName;
int edition;
printBookDetails(String name, int edition){
this.bookName = name;
this.edition = edition;
print('You are searching for a book named $bookName, Edition $edition');
}
}

Constructors

Dart defines a constructor with the same name as that of the class. Constructors are used to initialize objects when it is created. Constructor is a function and it can be parameterized. Unlike a function, constructors cannot have a return type. All class have own constructor but if we don’t declare or forget then Dart compiler will create default constructor automatically by passing the default value to the member variable. If we declare own constructor, then default constructor will be ignored.

Example :

var obj = new Book();

In the above code snippet, Book() is a constructor created and initialized to an object obj.

Exception handling

An exception is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the Application terminates abnormally. Every exception in Dart is a subtype of the pre-defined class Exception. These exceptions can be handled by using try and catch blocks. A code snippet can have more than one catch blocks. Below is the syntax for try/ catch block.

try{
// your code here
}catch(Exception e){
//Exception handling code here.
}

You can raise exception explicitly by using throw keyword.

Why Dart?

Dart is used to develop hybrid mobile applications in Flutter framework, used to develop web applications in AngularDart Framework and server side applications using DartVM framework.

This concludes this brief tour of the basics of Dart. I hope now you have a better understanding of the Dart code. If this article is helpful for you do clap.

Thankyou.

--

--

Tilak

Android App Developer | Flutter Mobile App Developer