Dart Basics in one article

Keval Prajapati
TheLeanProgrammer
Published in
7 min readDec 31, 2020

So first of all it's not a fictional book or article for you to read. Open a new tab and go to the Dartpad to try it yourself. That way it's more fun. Trust me.

Main method ⚔

Everything starts with the main method in a dart program. When your program executes, it looks for the main method. If you look at any flutter code you will see that there is always the main method.

Data Types

Dart has data types like any other programming language.

Integer —

Gif showing dart pad output

You can use integer datatype to store integers. (Haha obviously).

So you see a print statement. Yeah! that's the way we print stuff on the console. Similar to System.out.println() in Java or printf() in C.

Double —

we use the keyword ‘double’ for using double data type.

double a = 10.9;

Num —

But what if you don’t know what number you want to store? Dart is there to help you.

You can use num so dart will decide the datatype in runtime for you. As we see in the below picture, a is given an integer value so the runtime type of a is integer, and b is given 6.45 so the runtime type is double.

String —

The string is as it is in any other language. A string is used to store group of characters. Like your name, your girlfriend's name or your boyfriend's name.

String name = "Keval"; //thats how its done.

Or we can use single quotes.

String name = 'Keval';

But ATTENTION here.

if we do

String a = '7';
int b = 7;

These are not the same. Let's go deep

Looking at the console, they look the same but they are not.

Let's ask it to dart itself.

For this, we use bool data type.

Bool datatype is used for true or false values. We will get back to it later.

As you can see the statement a==b gives false. Means a is not equal to b.

As a is a String but b is a number. You can treat b as an integer number and add subtract multiply or divide it but you cannot do the same for a.

Bool —

As we discussed, bool is used to store true or false values. It is pretty useful.

Let's use if-else for the sake of showcasing the use of bool.

Here we give false value to the bool kevalhasgf.

We use an if-else statement to check if he is single or not.

So first if will get the value false so the statements inside the if block would not run and the statement inside the else would run and it would print

Keval is single.

var —

So, you don’t know the data type for your variable. Like, what if instead of storing true in kevalhasgf, someone may want to store a number like 2 or 3.

So if you don’t know the data type of the variable, you can use the var datatype. Dart will decide in runtime the data type for you.

Here you can see the same variable is used to store integer and String.

This is not the only situation you use var. You will learn as you use it.

Now lets talk about print

As you saw, a print statement is used to print stuff on the console.

Simple.

You can print variables or string.

print("this is string");
print(a);

But you cannot concat as in java using just the + sign. 😐

print("this is string with variable"+a); this will not work

You need to convert variable to string using .toString();

print("this is string with variable" + a.toString());//that works

The better approach. 😀

print("this is good $a");//the $ sign

You can use the $ sign to print a variable.

if-else

if-else is similar to most other languages.

if(condition){ statements;
}else{statements;}

Something interesting ❓

There is another way to do that

What’s that? Let’s simplify

the condition? what if true : what if false ;

Loops ➿

for loop

syntax

for(int i =0; i<10;i++){statement to repeat
}

this loop will repeat the statements inside while the value of i (starting from 0) is less than 10 but also increase the value to I with 1 every iteration.

break 💔

break — we can use the break to break a loop in between. Once the break is done, the loop will not continue next iterations.

Let's do a program to print integers but break when there i= 7.

As you see the loop breaks after 7.

But what if we want the loop to continue but not print 7?

We use continue statement,

As you see when i is equal to 7, the loop continues to the next iteration and ignores the statements below.

While loop

int x=0;
while(x<10){
print(x);
}

This will print 0 to 10.

List

List is what you think it is. It is just list of data.

It is similar to an array in java.

declaration of a list.

datatype listname = [elements, separated ,by, comma]; 
e.g.
int numbers = [1,2,3,4,5];

To access a value we use its index.

Index is a number given to its position. Like any other language, it starts with 0.

int numbers = [1,2,3,4,5];print(numbers[0]);
//this would give us 1

What if we want to do something for each item?

We use foreach loop.

As the name suggests, it does something for each item/element in the list.

Function — some will say method (me)

Function is a way to make code look good and readable.

main is also a function which is called first.

The function is like your younger brother. Just teach him how to do it once and tell him to do it numerous times like a slave. Sorry, I am a younger brother so…. 😅

We define a function ones

what_it_returns function_name(what_it_takes){what it does?
}

I will explain.

and you call it by taking its name.

function_name(what it takes);

lets do it with a example.

What if you want to teach your brother to turn on the tv?

first we teach him or define a function

See, now you don’t have to write the print statement always. Just call the function.

Function which requires

Now let's teach him to switch channels. even if you teach him how to switch channels with a remote, every time you need to give him the channel you want him to switch to.

Lets look at the function closely.

It specifies a requirement of an integer which is name num in the function.

While calling the method, we provide a integer (3).

The number of requirements is not limited to the one you can specify the number of requirements separated by a comma.

Return — -

What if you want him to tell the volume after switching.

We can use return to return a value after the function is executed.

See? We use a return statement to end the method and return a value that we set to an integer or use directly.

Class

Here I will suggest you rather request you to learn oops concept in a depth.

You will find number of tutorials or lectures. But please learn oops concepts before jumping to flutter.

To define a class we

class CLASS_NAME{
}

Example of dog class

class dog{String breed;int age;
}

For class with constructor

If you don’t know what is constructor , learn oops.

class dog{String breed;int age;//CONSTRUCTOR     dog(this.breed,this.age);}

Object of a class

To create an object of a class

Thanks.

According to me, this is enough to get you started with dart or flutter indeed. But please start that’s more important.

“Starting is much much better than just finding the best time to start” — Keval Prajapti (me) 😎😎

So long article.

Any query? connect to me on linkedIn — Keval Prajapati | LinkedIn

or google it (simple).

Was it boring? please tell me. I want feedback. Anyway I know it was boring :P

I seek to write more articles in future so please give me feedback.

Don’t forget to follow The Lean Programmer Publication for more such articles, and subscribe to our newsletter tinyletter.com/TheLeanProgrammer

--

--