0% found this document useful (0 votes)
282 views57 pages

Dart

The document provides an overview of Dart programming including setup, fundamentals, data types, operators, loops, functions, classes, and exception handling. It discusses installing the Dart SDK and Visual Studio Code. Key features of Dart covered include being open source, platform independent, supporting concurrency with isolates, flexible compilation to JavaScript, being type safe and object-oriented, and having extensive libraries. Common collection types like List, Set, Map are also mentioned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
282 views57 pages

Dart

The document provides an overview of Dart programming including setup, fundamentals, data types, operators, loops, functions, classes, and exception handling. It discusses installing the Dart SDK and Visual Studio Code. Key features of Dart covered include being open source, platform independent, supporting concurrency with isolates, flexible compilation to JavaScript, being type safe and object-oriented, and having extensive libraries. Common collection types like List, Set, Map are also mentioned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

Dart & Flutter

App Development
DART PROGRAMMING
Contents
• Setup
• Fundamentals
• Data Types
• String
• Type Conversion
• Constant
• Null
• Operators
• Loops
• Collection [List, Set, Map]
• Function
• Class
• Exception Handling
Setup Dart SDK
• To install Dart SDK first you visit- https://dart.dev
Install Dart SDK
Install Visual Studio Code
• https://code.visualstudio.com/
Open Dartpad
• https://dartpad.dev
Fundamentals
• What is Dart Programming
• Dart is a main programming language to develop cross platform
mobile application using flutter framework.
• Why Dart Programming
• Dart has a declarative and programmable layout that is easy to read
and visualize. Hence, Flutter doesn't require a separate declarative
layout language like XML. It is easy for Flutter to provide advanced
tooling since all the layout in one language and in a central place.
Why is Dart better than Java?
• Dart is a programming language used by flutter, flutter is used to
create cross-platform apps (for android & ios). If your plan is to only
create apps for android only you should definitely go with java, it will
run and look better in almost every way.
What is the advantage of Dart?
• Dart is an interesting programming language with features to facilitate
Web, mobile and command line apps. Its major advantages are
its stability and ease of learning. In the case of Web applications, the
combination with AngularDart makes it a very powerful tool.
• Four Reasons
• Dart enforces object-oriented programming
• Dart compilers are quick and reliable
• Dart has a clean and type-safe syntax
• Dart has a strong developer community
Dart Programming Features
Open Source

• Dart is an open-source programming language, which means it is


freely available. It is developed by Google, approved by the ECMA
standard, and comes with a BSD license.
Platform Independent

• Dart supports all primary operating systems such as Windows, Linux,


Macintosh, etc. The Dart has its own Virtual Machine which known as
Dart VM, that allows us to run the Dart code in every operating
system.
Concurrency

• Dart is an asynchronous programming language, which means it supports


multithreading using Isolates. The isolates are the independent entities that
are related to threads but don't share memory and establish the
communication between the processes by the message passing. The
message should be serialized to make effective communication. The
serialization of the message is done by using a snapshot that is generated
by the given object and then transmits to another isolate for desterilizing.
Concurrency
Flexible Compilation

• Dart's flexibility in compilation and execution doesn't stop there.


For example, Dart can be compiled into JavaScript so it can be
executed by browsers. This allows code reuse between mobile apps
and web apps. Developers have reported as high as 70% code
reuse between their mobile and web app
Type Safe

• The Dart language is type safe: it uses a combination of


static type checking and runtime checks to ensure that a
variable's value always matches the variable's static type,
sometimes referred to as sound typing. Although types are
mandatory, type annotations are optional because of type
inference
Object-Oriented

• Dart is an object-oriented programming language and supports all


oops concepts such as classes, inheritance, interfaces and optional
typing features. It also supports advance concepts like mixin, abstract,
classes, reified generic, and robust type system.
Extensive Libraries

• Dart consists of many useful inbuilt libraries including SDK (Software


Development Kit), core, math, async, math, convert, html, IO, etc. It
also provides the facility to organize the Dart code into libraries with
proper namespacing. It can reuse by the import statement.
Flexible Compilation

• Dart provides the flexibility to compile the code and fast as well. It
supports two types of compilation processes, AOT (Ahead of Time)
and JIT (Just-in-Time). The Dart code is transmitted in the other
language that can run in the modern web-brewers.
Objects

• The Dart treats everything as an object. The value which assigns to the
variable is an object. The functions, numbers, and strings are also an
object in Dart. All objects inherit from Object class.
Browser Support

• The Dart supports all modern web-browser. It comes with the dart2js
compiler that converts the Dart code into optimized JavaScript code
that is suitable for all type of web-browser.
Community

• Dart has a large community across the world. So if you face problem
while coding then it is easy to find help. The dedicated developers'
team is working towards enhancing its functionality.
Flutter Download
Get Started
Install
Key Points to Remember
• Everything in Dart is treated as an object including, numbers, Boolean,
function, etc. like Python. All objects inherit from the Object class.
• Dart tools can report two types of problems while coding, warnings
and errors. Warnings are the indication that your code may have some
problem, but it doesn't interrupt the code's execution, whereas error
can prevent the execution of code.
• Dart supports generic types, like List<int>(a list of integers)
or List<dynamic> (a list of objects of any type).
Final And Const Keyword in Dart:

• These keywords are used to define constant variable in Dart i.e. once a
variable is defined using these keyword then its value can’t be
changed in the entire code. These keyword can be used with or
without data type name
• Num a =5;
• Print(num);

• Double d= 4.8;
• Print(d);

• Var v= 3.6;

• Dynamic d = 34;
Syntax for final

// Without datatype

final variable_name

// With datatype final

data_type variable_name
Syntax for Const:

// Without datatype

const variable_name

// With datatype const

data_type variable_name
Example

void main() {
// Assigning value to geek1 variable without datatype
final geek1 = "Geeks For Geeks";
// Printing variable geek1
print(geek1);

// Assigning value to geek2 variable with datatype


final String geek2 = "Geeks For Geeks Again!!";
// Printing variable geek2
print(geek2);
}
Example
void main() {
// Assigning value to geek1 variable without datatype
const geek1 = "Geeks For Geeks";
// Printing variable geek1
print(geek1);

// Assigning value to geek2 variable with datatype


const geek2 = "Geeks For Geeks Again!!";
// Printing variable geek2
print(geek2);
}
By ‘ + ‘ Operator
• Example: Using ‘+’ operator to concatenate strings

void main() {

// Assigning values
// to the variable
String gfg1 = "Geeks";
String gfg2 = "For";

// Printing concatenated result


// by the use of '+' operator
print(gfg1 + gfg2 + gfg1);
}
By String Interpolation

• Example: Using string interpolation to concatenate strings 

void main() {

String gfg1 = "Geeks";


String gfg2 = "For";

// interpolation without space


print('$gfg1$gfg2$gfg1');

print('$gfg1 $gfg2 $gfg1');


}
By List_name.join() Method

list_name.join("input_string(s)");

This input_string(s) are inserted between each element of the


list in the output string.

Example: Using list_name.join() concatenate strings


• void main() {

• // Creating List of strings


• List<String> gfg = ["Geeks", "For" , "Geeks"];

• // Joining all the elements // of the list and // converting it to String


• String geek1 = gfg.join();

• // Printing the // concatenated string


• print(geek1);

• // Joining all the elements // of the list and converting // it to String


• String geek2 = gfg.join(" ");

• // Printing the // concatenated string


• print(geek2);
•}
Flow Statement

• Decision-making statements
• Looping statements
• Jump statements
Flow Statement
of Decision-making statement.
• Dart provides following types If Statement
• If-else Statements
• If else if Statement
• Switch Case Statement
Dart Looping Statements
• Dart looping statements are used to execute the block of code
multiple-times for the given number of time until it matches the given
condition. These statements are also called Iteration statement.
• Dart for loop
• Dart for….in loop
• Dart while loop
• Dart do while loop
Jump Statements

• Dart Break Statement


• Dart Continue Statement
Loops
• for loop
• for… in loop
• for each loop
• while loop
• do-while loop
for loop

• For loop in Dart is similar to that in Java and also the flow of
execution is the same as that in Java

for(initialization; condition; test expression){

// Body of the loop

}
Example

void main()
{
    for (int i = 0; i < 5; i++)
{
        print('GeeksForGeeks');
    }
}
for…in loop

for (var in expression)


{
// Body of loop
}
void main()
{
var ds = [ 1, 2, 3, 4, 5 ];
for (int i in ds {
print(i);
}
}
for each … loop

• The for-each loop iterates over all elements in some container/collectible


and passes the elements to some specific function

collection.foreach(void f(value))
• f( value): It is used to make a call to the f function for each element in the
collection.

void main() {

var ds = [1,2,3,4,5];

ds.forEach((var num)=> print(num));


}
Break and Continue

void main()
{
int count = 1;

while (count <= 10) {


print(“ds, you are inside loop $count");
count++;

if (count == 4) {
break;
}
}
print(“sh, you are out of while loop");
}
Dart Collection

• List<E>: A List is an ordered group of objects.


• Set<E>: Collection of objects in which each of the objects occurs only once
• Map<K, V>: Collection of objects that has a simple key/value pair-based object. The key and value of a map can be of
any type.
• Queue<E>: A Queue is a collection that can be manipulated at both ends. One can iterate over a queue with an Iterator
or using forEach.
• DoubleLinkedQueue<E>: Doubly-linked list based on the queue data structure.
• HashMap<K, V>: Map based on hash table i.e unordered map.
• HashSet<E>: Set based on hash table i.e unordered set.
• LinkedHashMap<K, V>: Similar to HashMap but based on LinkedList.
• LinkedHashSet<E>: Similar to HashSet but based on LinkedList.
• LinkedList<E extends LinkedListEntry<E>>: It is a specialized double-linked list of elements.
• LinkedListEntry<E extends LinkedListEntry<E>>: An element of a LinkedList.
• MapBase<K, V>: This is the base class for Map.
• UnmodifiableListView<E>: An unmodifiable List view of another List.
• UnmodifiableMapBase<K, V>: Basic implementation of an unmodifiable Map.
• UnmodifiableMapView<K, V>: Unmodifiable view of the map.
List<E>

• The list is an ordered group of objects where each object is from one
specific type. To define a list in dart, specify the object type inside the
angled brackets (<>) as shown below:

List<String> fruits = ["Mango", "Apple", "Banana"]


• void main() {
• // creating a new empty List
• List dsList = new List();

• // Adding an element to the dsList


• dsList.addAll([1,2,3,4,5,"Apple"]);
• print(dsList);

• // Looping over the list


• for(var i = 0; i<dsList.length;i++){
• print("element $i is ${dsList[i]}");
•}

• // Removing an element from dsList by index


• dsList.removeAt(2);
• // Removing an element from dsList by object
• dsList.remove("Apple");
• print(dsList);

• // Return a reversed version of the list


• print(dsList.reversed);

• // Checks if the list is empty


• print(dsList.isEmpty);

• // Gives the first element of the list


• print(dsList.first);

• // Reassigning the dsList and creating the


• // elements using Iterable
• dsList = Iterable<int>.generate(10).toList();
• print(dsList);
•}
Set<E>
• Sets are one of the essential part of Dart Collections. A set is defined
as an unordered collection of unique objects. To define a set follow the
below:
Set fruits = Set.from("Mango", "Apple", "Banana")
void main() {
Set dsSet = new Set();
dsSet.addAll([9,1,2,3,4,5,6,1,1,9]);
// Looping over the set
for(var el in dsSet){
print(el);
}
// length of the set.
print('Length: ${dsSet.length}');
// printing the first element in the set
print('First Element: ${dsSet.first}');
// Deleting an element not present. No Change
dsSet.remove(10);
// Deleting an element 9
dsSet.remove(9);
print(dsSet);
}
Map<K, V>

• In Dart, Maps are unordered key-value pair collection that sets an


associate key to the values within. To define a Map, specify the key
type and the value type inside the angle brackets(<>) as shown below

Map<int, string> fruits = {1: "Mango", 2:"Apple", 3:"Banana"}


void main() {

// Initializing the map with sample values.


var dsMap = {1:"Apple",2:"Mango",3:"Banana"};
print(dsMap);

// Adding elements by different methods.


dsMap.addAll({4:'Pineapple',2:'Grapes'});
dsMap[9]="Kiwi";
print(dsMap);

// printing key and values


print('Keys: ${dsMap.keys} \nValues: ${dsMap.values}');

// removing an element from the map by its key


dsMap.remove(2);

// printing the map and its length


print('{$ dsMap} length is ${dsMap.length}’);
}

You might also like