Dart basic concepts to keep in mind while developing the application

Pravin Palkonda
9 min readJan 11, 2023

--

As we start learning about flutter, the main important thing is we should get clear all the concepts of the dart because flutter uses dart programming language. Many people say that documentation is the best source to learn and understand any language. It's good practice to learn through the documentation but it contains lots of data that we feel we have to learn lots of things and it will take a lot of time to complete it.

This article will be about basic dart concepts that will help you to start with the development of the application.

While learning flutter, one question arises why flutter uses dart but not any other language?

Dart allows flutter to avoid the need of separate declarative layout language like JSX and XML. The layout of the dart is declarative and programmatic, which makes developers easy to read and visualize it quickly and effortlessly. To understand it in details click here.

What is a dart?

Dart is a programming language that is used to build an application for any platform using the flutter framework.

Dartpad is the best platform to practice dart programming language. No need to install anything. Just head out to the dartpad official website and start practicing dart.

Let's dive into variables used in dart

syntax to declare variable → ( type variable_name; )

syntax to create a variable and declare it → ( var name=’abc’; )

var

var is used to declare a variable without specifying its type

void main() {
var data=10;// declaring and initializing var without its type
print(data);// printing the the value
// output=10
print(data.runtimeType); // printing the type
// output =int
}

int

int is a data type that indicates it is a number without any decimal point .

For example — ‘10’ is an number without any decimal which is represented using the int keyword.

void main() {
int age=10;// declaring and initializing var of integer type
print(age);// printing the the value
}

double

double is a data type that represents it is a number with a decimal point.

For example — ‘12.8’ is a number with a decimal value which is represented using a double keyword

void main() {
double price=12.25;// declaring and initializing a double value with 12.25
print(price);// printing the price
// output = 12.25
}

String

A string is a data type, which indicates a sequence of characters that is used to store data like name, address, etc. using double or single quotes.

For example — “data” is a string that is represented using double quotes using keyword String.

void main() {
String name = "Flutter and dart";// declaring and initialixing string
print(name);
}

boolean

boolean is a data type used to represent two values such as true or false.

In dart, we use the bool keyword to represent a boolean value.

The value of the boolean must be initialized first else it will throw an error.

void main() 
bool isValid=false;// declaring and initializing boolean
print(isValid);// prints false
}

dynamic

dynamic is a special kind of data type which is used to declare a variable that can store data implicitly and value during running the program.

It is similar to var but when we use var and assign any value then it is replaced by the appropriate data type.

By using dynamic keyword we can change the data type dynamically.

void main() {
var name = "demo"; // declaring and initializing var with vaue String
print(name.runtimeType); // it will print String type
dynamic address = "addres.. "; //declaring and initializing dynamic variable
print(address.runtimeType); // it will print String type
address = 12; // re-assigning integer value to address
print(address.runtimeType); // it will print int
}

final and const

final and const keyword are used when you don't want the change the value in the entire code.

These keywords are used to declare constants which can be declared with data type of without data type.

Once it is declared using final or const keyword then its value cannot be changed again.

void main() {

// without data type
final name="Demo name";
print(name);
// with data type string
final String address="Demo address";
print(address);
/// if we, re-assign the value for name and address it will throw error
/// as the final variable can only set once


// without data type
const age=12;
print(age);
// with data type int
const int mobile=123456;
print(mobile);
/// if we, re-assign the value for mobile and age it will throw error
/// as the constant variable can't assigned a value

}

List

A list is a data type that is used to represent a collection of an object.

In dart, we use the List keyword to indicate it as a list with or without any data type.

void main() {
/// without any data type
/// we can have number,double, string to the list as the data type is not defined
List dynamicDataList = ["apple", "banana", 1, 1.2];
print(dynamicDataList);

/// This list can have only string as data type of list is defined
List<String> listOfString = ["apple", "banana"];
print(listOfString);
}

Let's understand some Control Flow: conditional statements

conditional statements are used to execute the program based on the conditions.

if statement

if statement is used to execute a specific block of code based on the condition given in the if statement.

Syntax → if (condition) block of code

void main() {
if (5 > 3) print("Hello dart");
/// it will print Hello dart as 5 is greate than 3

if(2>3){
print("Hello world");
/// this code of block will not execute as 2 is not greater than 3
}
}

if .. else statement

if .. else statement is used to execute any one of the block of code based on the condition given. It contains two blocks of code (if block of code and else block of code).

syntax → if(condition){ if block of code } else {else block of code}

void main() {
if (2 > 4) {
print("Hello world");
} else {
print("Hello dart");
}
/// in this example else statement is printed as
/// 2 is not greater than 4
}

else .. if ladder statement

else.. if ladder is used when we want to check multiple conditions to execute a specific block of code.

syntax → if (condition1){ block 1 }else if(condition2 ){ block 2} else if (condition3){ block 3 }…

void main() {
if (2 > 4) {
print("Hello world");
} else if (2<4){
print("Hello dart");
}else if (2==2){
print("Equal");
}
/// in this example hello dart is printed as 2 is smaller than 4
/// it will not further else if condition and does not execute any further block of code

}

switch case statement

If you wanted to avoid multiple use of if-else statement then we can use switch case. We can use switch case to execute specific block of code by passing the matching case.

void main() {

int n=2;
switch (n) {
case 0:
print("It is zero");
break;
case 1:
print("It is one");
break;
case 2:
print("It is two");
break;
default:
print("It is default");
}

/// in this example it is two will be printed.
/// we have to use break statement after each case else all of the
/// case will be executed till end of the program.
/// If any of the cases does not get matched then default case gets executed
}

Let's explore some Loops

If we want to execute a block of code a specific number of times or till matches any condition then in dart we use loops.

for loop

The for loop is used when we want to execute a block of code for n number of times, here we know the exact value of n.

syntax → for (initialization;condition;increment/decrement){ block of code}

void main() {
for (int i = 1; i <= 10; i++) {
print("Number $i");
}
/// here the code will execute for 10 times.
/// as i is increament,it will start from i=1 till i=10.
/// printing i from 1 to 10.
}

for in

for in loop will execute a code till the elements present in the expression.Here the expression is a iterator.

syntax → for(var i in expression) { block of code }

void main() {
var list = ['a', 'b', 'c', 'd'];/// initializing a list

for (var i in list) {
print("Its $i");/// print all the elements present in the list
}
}

In dart we also have while loop and do-while loop but for loop is used mostly.

OOPs concepts

Dart is an object-oriented programming language that supports all the concepts of object-oriented programming language such as classes, objects, inheritance, etc.

Class

Class is a blueprint of the object which contains variables, constructors and methods.

class keyword id used to define a class.

To get all the properties of class an object of the class is created.

syntax → class ClassName { properties of class }


/// defining a class
class Demo {
String name = "demo";/// string variable of class

/// method of the class
/// this method is called by creating object of the class
printSomething() {
print("Printing something...");
}
}

Object

An object is an instance of the class which is used to access the properties od class.

Theoriticall objects are real life entity which has state and behaviour.

To access the properties of class we must create an object of the class.

syntax → class_name = object_name class_name();

void main() {

var demo = Demo();/// creating object of the class Demo

demo.printSomething();/// calling printSomething method using object
}

class Demo {
String name = "demo";/// string variable of class

/// method of the class
printSomething() {
print("Printing something...");
}
}

Inheritance

The ability to create a new class from the existing class is called inheritance.

In dart we use extend keyword to inherit the class.

The newly created class is called child class and class which is inherited by another class is called parent class.

syntax → class child_class_name extends parent_class_name

void main() {
Abc abc = Abc();/// creating object of child class

/// assigning the value to variable which is defined in parent class
/// using child class object becuase child class inherits the properties
/// of parent vlass
abc.fruitName = "Banana";
abc.fruitPrice = 20;
/// calling function of parent class using child class object
abc.printName();
abc.printPrice();
/// calling function of child class
abc.taste();
}

/// parent class
class Fruit {
String? fruitName;
int? fruitPrice;


/// function to print name of the fruit
printName() {
print("The fruite name is $fruitName");
}

/// function to print price of the fruit
printPrice() {
print("The fruite price is $fruitPrice");
}
}

/// child class
class Abc extends Fruit {
taste() {
print("Taste is sweet");
}
}

output

The fruite name is Banana
The fruite price is 20
Taste is sweet

In dart, we also have a types of inheritance like multilevel inheritance,etc.

Click here to explore more oops concepts in detail.

Functions

Function is set of a code which performs specific task.

When we want divide large amount code into smaller parts of code then we use functions. Creating a function is a good practice that code is optmized, easily readable and also it can reusable.

syntax → return_type function_name(parameters){ return value;}

void main() {
/// creating function with parameters and return type
int sumOf(int a, int b) {
return a + b;/// returning a value
}
/// here return type and type of return value should match

int c = sumOf(2, 3);/// calling function with parameters value 2 and 3
print("Sum is .. $c");



/// creating a function withount any parameters and return type
getSomething(){
return "Hello dart";
}

var something=getSomething();
print("It is $something");


}

future

future represents that it will receive a value which will be available in future and that value may complete value or any error value. It mostly works with asynncronous programming.

Mostly future is used when we are fetching the data through the server,etc

void main() {
printSomething();/// calling future function
print('Printing from main...');/// this will print first
}

Future printSomething() {
return Future.delayed(const Duration(seconds: 2), () {
return print("Printing something from future...");/// this will print after 2 seconds
});
}

/// here we are using future function which represents it will receive
/// value in future. Hence print statement in future function is printed
/// after the print statement in main function because of the delay of 2 seconds

async and await

async and are keyword which used for asynchronous program.

We have to use aync keyword before function to make it async function.

await keyword is used to get the result of asynchronous expression and it only works with async function.

/// async function
printSomething() async {
print("Print something");
}

/// making main function asynchronous using async keyword
void main() async {
await printSomething(); // Using await keyword calling printSomething method
print("Print something from main");
}

In this article, we have completed most of the basics of dart which is sufficient for the development of the application. Still, there are lots of concepts that can be cleared while working on the application or you can go through the official documentation.

Let me know in the comments if I need to correct anything. I will try to improve it.

Clap if you like the article. 👏

--

--