Starting with Dart for Flutter

Akshay Sachdeva
5 min readMar 12, 2020

--

In the previous article, we created a basic flutter application and understand a little bit about what’s going on. In this article, we are going to learn about Dart a language developed by google on which flutter is based. We are going to get deeper into the language and understand the basic concepts of Dart so that you can write the complex application in Flutter, So let’s get started with the basic introduction to Dart.

**If you don’t have your development environment setup you can try Dartpad DartPad is an open-source tool that lets you play with the Dart language in any modern browser. Many pages in this site — especially codelabs — have embedded DartPads. To get a DartPad as big as your browser window, go to the DartPad site.

Introduction

Dart is a client-side optimized object-oriented programming language developed by Google used to build mobile, server, and web applications and can compile either to native code or javascript, it first appeared in October 2011, the syntax of Dart is very similar to other popular programming languages like C++, Java, and JavaScript.

Main Method

The main part of any Dart program is the main function which acts as an entry point to your application, whenever the dart compiler compiles any program its first search for the main function. We can create a main function like this -

main(){
// Something Interesting is coming
}

The main function is the most important to get started with Dart.
We will get back to the functions or method but like any other object-oriented programming language, we should first understand the concept of classes.

Classes

In the context of Dart, classes are templates that are used to create objects and to define object data types and methods, it binds the method, variables and provides encapsulation. We can create a class in dart by the keyword class associating with a name and curly braces.

class nameOfTheClass {

}

When you create a class dart compiler implicitly create a method or a function without any return type and any arguments that method is called a constructor.

Methods

To understand the above let’s first understand what is a method.

A method, in general, is a procedure, technique, or way of doing something. Similarly in Dart, a method is a piece of a container in which we specify what we want to do. The signature of a method is like something like this -:

returnType nameOfTheMethod(Args if any){

}

Return Type is what we want to get after that block completes its work like String, Integer or boolean if you don’t want to return anything we can just write void, void means that the method does not return anything.

//Method returning nothing

void doSomething(){

}

//Method returning String

String returnSomething(){

return “hello”;

}

Constructor

Going back to constructors, A constructor is a special method without any return type and has the same name as the class, Constructors are responsible for creating objects, in Dart programming language we can create 3 types of constructors :

  1. Default Constructor (With no arguments)
  2. Parameterized constructor
  3. Named Constructor

**The constructor is the first thing that executes in a class.

Default Constructors — Default constructors are created implicitly by the dart compiler if you don’t specify any constructor that constructor they don’t have any arguments.

class ExampleDefaultCostructor{

ExampleDefaultConstructor(){

// Default constructor looks something like this

}

}

Parameterized Constructor — Constructors can also take arguments, which are used to initialize global variables. A constructor that takes parameter is known as parameterized constructor. When we call the parameterized constructor we have to pass the arguments with the same type as that constructor needs, To create the object of the class with parameterized we have to pass the arguments.

class ExampleParameterizedConstructor{

ExampleParameterizedConstructor(String name){

print(name); //Just printing the string

}

}

void main(){

ExampleParameterizedConstructor(“My Name”);

}

Named Constructors — Dart provides named constructors to enable a class defines multiple constructors. The syntax of named constructors is as given below −

class ExampleNamedConstructor {

ExampleNamedConstructor() {

print(“Named Constructor”);

}

ExampleNamedConstructor.namedConst(String name) {

print(“The engine is : ${name}”);

}

}

This Keyword

This keyword refers to the current instance of the class.

when the parameter name and the name of the class’s field are the same this keyword is used to remove the ambiguity.

class className {

String name; //Global variable

String age; //Global Variable

className( String name,String age){ //Local Variables

Global Variable ← this.name = name; → Local Variable

Global Variable ←this.age = age; → Local Variable

}

}

Data Types

Any programming language has the concept of data types. Data types tell the compiler the type value a variable will take like if the variable should take a number, character, Double or a List.

Numbers are used to represents the numerical values in dart there are basically two types of numbers either the number can be an integer or a decimal number.

Integer: Integer represents the nondecimal value in Dart like 10 20 or any other integer in maths. We can declare a variable of int type by the keyword int. The maximum size of an integer is 64 bit. That means, it is in the range -²⁶³ to ²⁶³ — 1.

int age = 10;

Double: Double represents the decimal or fractional value in Dart like 10.0 or 33.8989. We can declare a variable of type double by double keyword. Doubles are IEEE 754 standard floating-point numbers. The size of the double is 64 bit.

double height = 54.68;

Boolean: Boolean or bool is a data type that has two possible values: it is either true or false. We can declare a variable of type double by bool keyword.

bool isGiving = true;

Strings: Strings represent a sequence of characters. For instance, if you were to store some data like name, address, etc. the string data type should be used.

String name = “My Name is Jhon”;

Creating Objects

To create an instance of the class, use the new keyword followed by the class name. The syntax for the same is given below −

className randomName = new Construntor();

To call a method of the class we can call it by the reference variable like

randomName.methodName();

Now we have a basic understanding lets end this article by making a simple dart program that prints your name in the console using all the concepts we learned today.

Hint: print(); // Method is used to print something on console

class MyClassName{
String name;
MyName(String name){
this.name = name;
}
String printName(){
print(name);
}
}

void main(){
MyClassName myRefrenceVar = new MyClassName(“Akshay”);
myRefrenceVar.printName();
}

In this article we created a basic program that outputs your name on the console and understands the basic concepts of Dart, In the next article we will take a deep dive into more complex concepts of dart programming language meanwhile stay tuned and Happy Coding.

--

--

No responses yet