Chapter 2
Chapter 2
Chapter 2
There are multiple ways to install a dart on your system. You can install Dart on
Windows, Mac, and Linux or run it from the browser.
Dart Windows Installation
Follow the below instructions to install a dart on the windows operating system.
Steps:
Download Dart SDK from here.
Copy dart-sdk folder to your C drive.
Add C:\dart-sdk\bin to your environment variable. Watch the video below to
be more clear.
Open the command prompt and type dart --version to check it.
Install VS Code and Add Dart Extension.
Syntax
This is syntax for creating a variable in dart.
type variableName = value;
void main() {
// declaring variables // printing variables value
print("Name is $name");
String name = "John"; print("Address is $address");
String address = "USA"; print("Age is $age");
print("Height is $height");
num age = 20; // used to store any types
of numbers print("Married Status is $isMarried");
}
num height = 5.9;
bool isMarried = false;
Syntax
try {
// Your Code Here
}
catch(ex){
// Exception here
}
void main(){
add(10, 20);
}
void main(){
Set<String> fruits = {"Apple", "Orange", "Mango"};
print(fruits);
}
MOBILE APPLICATION DEVELOPMENT 20
Dart Collection
Map In Dart
How To Create Map In Dart
void main(){
Map<String, String> countryCapital = {
'USA': 'Washington, D.C.',
'India': 'New Delhi',
'China': 'Beijing'
};
print(countryCapital);
}
MOBILE APPLICATION DEVELOPMENT 21
Dart Collection
Map In Dart
Access Value From Key
void main(){
Map<String, String> countryCapital = {
'USA': 'Washington, D.C.',
'India': 'New Delhi',
'China': 'Beijing'
};
print(countryCapital["USA"]);
}
MOBILE APPLICATION DEVELOPMENT 22
Dart File Handling
Reading Assignment
Read File in Dart,
Write File in Dart,
Delete File in Dart,
void display() {
print("Animal name: $name.");
print("Number of Legs: $numberOfLegs.");
print("Life Span: $lifeSpan.");
}
} MOBILE APPLICATION DEVELOPMENT 26
Object-Oriented Programming in Dart
Object In Dart
Syntax
ClassName objectName = ClassName();
Create a class Camera with properties: name, color, megapixel. Create a method called display
which prints out the values of the three properties. Create two objects of the class Camera and
call the method display.
Encapsulation in Dart
Getter and Setter Methods
Getter and setter methods are used to access and update the value of private property. Getter
methods are used to access the value of private property. Setter methods are used to update the
value of private property.
Syntax
class ParentClass {
// Parent class code
}
class ChildClass extends ParentClass {
// Child class code
}
void main() {
int age = null; // give error
}
Programmers do have a lot of difficulties while handling null values. They forget
that there are null values, so the program breaks. In real world null mostly acts
as time bomb for programmers, which is ready to break the program.