Dart Programming : Part-4 : Functions & Collection

Jayesh Pansheriya
Analytics Vidhya
Published in
3 min readDec 31, 2019

Functions are the building blocks of readable, maintainable, and reusable code. A function is a set of statements to perform a specific task. Functions organize the program into logical blocks of code. Once defined, functions may be called to access code. This makes the code reusable. Moreover, functions make it easy to read and maintain the program’s code.

void main() { 
print(factorial(6));
}
factorial(number) {
if (number <= 0) {
// termination case
return 1;
} else {
return (number * factorial(number - 1));
// function invokes itself
}
}
Output :
720

Lambda Functions

Lambda functions are a concise mechanism to represent functions. These functions are also called as Arrow functions.

oid main() { 
printMsg();
print(test());
}
printMsg()=>
print("hello");

int test()=>123;
Output :
hello 123

Interfaces

An interface defines the syntax that any entity must adhere to. Interfaces define a set of methods available on an object. Dart does not have a syntax for declaring interfaces. Class declarations are themselves interfaces in Dart.

Classes should use the implements keyword to be able to use an interface. It is mandatory for the implementing class to provide a concrete implementation of all the functions of the implemented interface. In other words, a class must redefine every function in the interface it wishes to implement.

void main() { 
ConsolePrinter cp= new ConsolePrinter();
cp.print_data();
}
class Printer {
void print_data() {
print("__________Printing Data__________");
}
}
class ConsolePrinter implements Printer {
void print_data() {
print("__________Printing to Console__________");
}
}
Output :
__________Printing to Console__________

Collection

List

A List is simply an ordered group of objects. The dart:core library provides the List class that enables creation and manipulation of lists.

  • Fixed Length List − The list’s length cannot change at run-time.
  • Growable List − The list’s length can change at run-time.

Set

Set represents a collection of objects in which each object can occur only once. The dart:core library provides the Set class to implement the same.

Maps

The Map object is a simple key/value pair. Keys and values in a map may be of any type. A Map is a dynamic collection. In other words, Maps can grow and shrink at runtime. The Map class in the dart:core library provides support for the same.

Queue

A Queue is a collection that can be manipulated at both ends. Queues are useful when you want to build a first-in, first-out collection. Simply put, a queue inserts data from one end and deletes from another end. The values are removed / read in the order of their insertion.

Exceptions

The try block embeds code that might possibly result in an exception. The on block is used when the exception type needs to be specified. The catch block is used when the handler needs the exception object.

The try block must be followed by either exactly one on / catch block or one finally block (or one of both). When an exception occurs in the try block, the control is transferred to the catch.

main() { 
try {
test_age(-2);
}
catch(e) {
print('Age cannot be negative');
}
}
void test_age(int age) {
if(age<0) {
throw new FormatException();
}
}
Output :
Age cannot be negative

How Can You Contribute?

Posts In This Series

Show Your Support

Press the clap button below if you liked reading this post. The more you clap the more it motivates me to write better!

--

--