07-Function
playground29.dart
Code
void main() {
showOutput(square(2));
showOutput(square(2.5));
print(square.runtimeType);
}
dynamic square(var num) {
return num * num;
}
void showOutput(var msg) {
print(msg);
}
Output
4
6.25
(dynamic) => dynamic
playground30.dart
Code
void main() {
// Arrow Function =>
showOutput(square(2));
showOutput(square(2.5));
}
dynamic square(var num) => num * num;
void showOutput(var msg) {
print(msg);
}
Output
4
6.25
playground31.dart
Code
main() {
var list = ['apples', 'bananas', 'oranges'];
list.forEach(printF);
}
void printF(item) {
print(item);
}
Output
apples
bananas
oranges
playground32.dart
Code
main() {
var list = ['apples', 'bananas', 'oranges'];
list.forEach((item) {
print(item);
});
}
Output
apples
bananas
oranges
playground33.dart
Code
void main() {
print(sum(2, 2));
}
dynamic sum(var num1, var num2) => num1 + num2;
Output
4
playground34.dart
Code
void main() {
print(sum(num2: 4, num1: 2));
}
dynamic sum({var num1, var num2}) => num1 + num2;
Output
6
playground35.dart
Code
void main() {
print(sum(10, num2: 2));
}
dynamic sum(var num1, {var num2}) => num1 + num2;
Output
12
playground36.dart
Code
void main() {
print(sum(10));
print(sum(10, num2: 2));
}
dynamic sum(var num1, {var num2 = 0}) => num1 + num2;
Output
10
12
08-Class
playground37.dart
Code
class Person {
String name;
int age;
void showOutput() {
print(name);
print(age);
}
}
void main() {
Person person1 = Person();
person1.name = 'Mahmud';
person1.age = 35;
person1.showOutput();
}
Output
Mahmud
35
playground38.dart
Code
class Person {
String name;
int age;
Person(String name, [int age = 18]) {
this.name = name;
this.age = age;
}
void showOutput() {
print(name);
print(age);
}
}
void main() {
Person person1 = Person('Mahmud');
person1.showOutput();
Person person2 = Person('Mahmud', 35);
person2.showOutput();
}
Output
Mahmud
18
Mahmud
35
playground39.dart
Code
class Person {
String name;
int age;
Person(this.name, [this.age = 18]);
void showOutput() {
print(name);
print(age);
}
}
void main() {
Person person1 = Person('Mahmud');
person1.showOutput();
Person person2 = Person('Mahmud', 35);
person2.showOutput();
}
Output
Mahmud
18
Mahmud
35
playground40.dart
Code
class Person {
String name;
int age;
Person(this.name, [this.age = 18]);
// named constructor
Person.guest() {
name = 'Guest';
age = 19;
}
void showOutput() {
print(name);
print(age);
}
}
void main() {
Person person1 = Person('Mahmud');
person1.showOutput();
var person2 = Person('Jack', 35);
person2.showOutput();
var person3 = Person.guest();
person3.showOutput();
}
Output
Mahmud
18
Jack
35
Guest
19
playground41.dart
- final: type will be defined by inferred value
Code
class X {
final name;
static const int age = 10;
X(this.name);
}
void main() {
var x = X('Jack');
print(x.name);
print(X.age);
/* Wrong way
x.name = 'Jill';
print(x.name);
*/
var y = X('Jill');
print(y.name);
}
Output
Jack
10
Jill
playground42.dart
Code
// Class
class Vehicle {
String model;
int year;
Vehicle(this.model, this.year) {
print(this.model);
print(this.year);
}
void showOutput() {
print(model);
print(year);
}
}
class Car extends Vehicle {
double price;
Car(String model, int year, this.price) : super(model, year);
void showOutput() {
print(this.price);
}
}
void main() {
var car1 = Car('Accord', 2014, 150000);
car1.showOutput();
}
Output
Accord
2014
150000.0
playground43.dart
- Define two calculated properties: right and bottom.
Code
class Rectangle {
num left, top, width, height;
Rectangle(this.left, this.top, this.width, this.height);
num get right => left + width;
set right(num value) => left = value - width;
num get bottom => top + height;
set bootom(num value) => top = value - height;
}
void main() {
var rect = Rectangle(3, 4, 20, 15);
print(rect.left);
print(rect.right);
print(rect.bottom);
rect.right = 12;
print(rect.left);
print(rect.top);
rect.bootom = 6;
print(rect.top);
}
Output
3 // left
23 // right = left + width = 3 + 20
19 // bottom = top + height = 4 + 15
-8 // left = value - width = 12 - 20
4 // top = 4
-9 // top = value - height = 6 - 15
09-Exception Handling
playground44.dart
Code
int mustGreaterThanZero(int val) {
if (val <= 0) {
throw Exception('Value must be greater than zero');
}
return val;
}
void letVerifyTheValue(var val) {
var valueVerification;
try {
valueVerification = mustGreaterThanZero(val);
} catch (e) {
print(e);
} finally {
if (valueVerification == null) {
print('Value is not accepted');
} else {
print('Value verified: $valueVerification');
}
}
}
void main() {
letVerifyTheValue(10);
letVerifyTheValue(0);
}
Output
Value verified: 10
Exception: Value must be greater than zero
Value is not accepted