diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml
deleted file mode 100644
index a55e7a1..0000000
--- a/.idea/codeStyles/codeStyleConfig.xml
+++ /dev/null
@@ -1,5 +0,0 @@
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/Dart_SDK.xml b/.idea/libraries/Dart_SDK.xml
deleted file mode 100644
index 2255dbd..0000000
--- a/.idea/libraries/Dart_SDK.xml
+++ /dev/null
@@ -1,26 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
deleted file mode 100644
index 639900d..0000000
--- a/.idea/misc.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
deleted file mode 100644
index d08c8ed..0000000
--- a/.idea/modules.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
deleted file mode 100644
index 94a25f7..0000000
--- a/.idea/vcs.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/02_built_in_data_types.dart b/02_built_in_data_types.dart
index 5f1e485..c7880fa 100644
--- a/02_built_in_data_types.dart
+++ b/02_built_in_data_types.dart
@@ -1,27 +1,27 @@
+// ignore_for_file: unused_local_variable
void main(List arguments) {
+ // Numbers: int
+ int score = 23;
+ var count = 23; // It is inferred as integer automatically by compiler
+ int hexValue = 0xEADEBAEE;
- // Numbers: int
- int score = 23;
- var count = 23; // It is inferred as integer automatically by compiler
- int hexValue = 0xEADEBAEE;
+ // Numbers: double
+ double percentage = 93.4;
+ var percent = 82.533;
+ double exponents = 1.42e5;
- // Numbers: double
- double percentage = 93.4;
- var percent = 82.533;
- double exponents = 1.42e5;
+ // Strings
+ String name = "Henry";
+ var company = "Google";
- // Strings
- String name = "Henry";
- var company = "Google";
+ // Boolean
+ bool isValid = true;
+ var isAlive = false;
- // Boolean
- bool isValid = true;
- var isAlive = false;
+ print(score);
+ print(exponents);
- print(score);
- print(exponents);
-
- // NOTE: All data types in Dart are Objects.
- // Therefore, their initial value is by default 'null'
+ // NOTE: All data types in Dart are Objects.
+ // Therefore, their initial value is by default 'null'
}
diff --git a/03_string_and_string_interpolation.dart b/03_string_and_string_interpolation.dart
index 3132427..fb3814d 100644
--- a/03_string_and_string_interpolation.dart
+++ b/03_string_and_string_interpolation.dart
@@ -1,32 +1,30 @@
+// ignore_for_file: unused_local_variable
void main() {
+ // Literals
+ var isCool = true;
+ int x = 2;
+ "John";
+ 4.5;
- // Literals
- var isCool = true;
- int x = 2;
- "John";
- 4.5;
+ // Various ways to define String Literals in Dart
+ String s1 = 'Single';
+ String s2 = "Double";
+ String s3 = 'It\'s easy';
+ String s4 = "It's easy";
- // Various ways to define String Literals in Dart
- String s1 = 'Single';
- String s2 = "Double";
- String s3 = 'It\'s easy';
- String s4 = "It's easy";
+ String s5 = 'This is going to be a very long String. '
+ 'This is just a sample String demo in Dart Programming Language';
- String s5 = 'This is going to be a very long String. '
- 'This is just a sample String demo in Dart Programming Language';
+ // String Interpolation : Use ["My name is $name"] instead of ["My name is " + name]
+ String name = "Kevin";
+ print("My name is $name");
+ print("The number of characters in String Kevin is ${name.length}");
- // String Interpolation : Use ["My name is $name"] instead of ["My name is " + name]
- String name = "Kevin";
+ int l = 20;
+ int b = 10;
- print("My name is $name");
- print("The number of characters in String Kevin is ${name.length}");
-
-
- int l = 20;
- int b = 10;
-
- print("The sum of $l and $b is ${l + b}");
- print("The area of rectangle with length $l and breadth $b is ${l * b}");
+ print("The sum of $l and $b is ${l + b}");
+ print("The area of rectangle with length $l and breadth $b is ${l * b}");
}
diff --git a/04_constants.dart b/04_constants.dart
index f171c16..9063da9 100644
--- a/04_constants.dart
+++ b/04_constants.dart
@@ -1,19 +1,18 @@
+// ignore_for_file: unused_local_variable
void main() {
+ // final
+ final cityName = 'Mumbai';
+ // name = 'Peter'; // Throws an error
- // final
- final cityName = 'Mumbai';
- // name = 'Peter'; // Throws an error
+ final String countryName = 'India';
- final String countryName = 'India';
-
- // const
- const PI = 3.14;
- const double gravity = 9.8;
+ // const
+ const PI = 3.14;
+ const double gravity = 9.8;
}
class Circle {
-
- final color = 'Red';
- static const PI = 3.14;
+ final color = 'Red';
+ static const PI = 3.14;
}
diff --git a/06_conditional_expressions.dart b/06_conditional_expressions.dart
index c0f3751..c790688 100644
--- a/06_conditional_expressions.dart
+++ b/06_conditional_expressions.dart
@@ -1,26 +1,22 @@
-
void main() {
+ // Conditional Expressions
- // Conditional Expressions
-
- // 1. condition ? exp1 : exp2
- // If condition is true, evaluates expr1 (and returns its value);
- // otherwise, evaluates and returns the value of expr2.
-
- int a = 2;
- int b = 3;
-
- int smallNumber = a < b ? a : b;
- print("$smallNumber is smaller");
+ // 1. condition ? exp1 : exp2
+ // If condition is true, evaluates expr1 (and returns its value);
+ // otherwise, evaluates and returns the value of expr2.
+ int a = 2;
+ int b = 3;
+ int smallNumber = a < b ? a : b;
+ print("$smallNumber is smaller");
- // 2. exp1 ?? exp2
- // If expr1 is non-null, returns its value; otherwise, evaluates and
- // returns the value of expr2.
+ // 2. exp1 ?? exp2
+ // If expr1 is non-null, returns its value; otherwise, evaluates and
+ // returns the value of expr2.
- String name = null;
+ var name = null;
- String nameToPrint = name ?? "Guest User";
- print(nameToPrint);
+ String nameToPrint = name ?? "Guest User";
+ print(nameToPrint);
}
diff --git a/07_switch_and_case.dart b/07_switch_and_case.dart
index ce90445..c144c91 100644
--- a/07_switch_and_case.dart
+++ b/07_switch_and_case.dart
@@ -1,28 +1,26 @@
-
void main() {
+ // Switch Case Statements: Applicable for only 'int' and 'String'
- // Switch Case Statements: Applicable for only 'int' and 'String'
-
- String grade = 'A';
+ String grade = 'A';
- switch (grade) {
+ switch (grade) {
+ case 'A':
+ print("Excellent grade of A");
+ break;
- case 'A':
- print("Excellent grade of A");
- break;
+ case 'B':
+ print("Very Good !");
+ break;
- case 'B':
- print("Very Good !");
- break;
+ case 'C':
+ print("Good enough. But work hard");
+ break;
- case 'C':
- print("Good enough. But work hard");
- break;
+ case 'F':
+ print("You have failed");
+ break;
- case 'F':
- print("You have failed");
- break;
- default:
- print("Invalid Grade");
- }
+ default:
+ print("Invalid Grade");
+ }
}
diff --git a/08_for_loop.dart b/08_for_loop.dart
index 01e767a..78af10e 100644
--- a/08_for_loop.dart
+++ b/08_for_loop.dart
@@ -1,22 +1,20 @@
-
void main() {
+ // FOR Loop
- // FOR Loop
-
- // WAP to find the even numbers between 1 to 10
-
- for (int i = 1; i <= 10; i++) {
-
- if ( i % 2 == 0) {
- print(i);
- }
- }
+ // WAP to find the even numbers between 1 to 10
+ for (int i = 1; i <= 10; i++) {
+ if (i % 2 == 0) {
+ print(i.toString() + ' par');
+ } else {
+ print(i.toString() + ' impar');
+ }
+ }
- // for ..in loop
- List planetList = ["Mercury", "Venus", "Earth", "Mars"];
+ // for ..in loop
+ List planetList = ["Mercury", "Venus", "Earth", "Mars"];
- for (String planet in planetList) {
- print(planet);
- }
+ for (String planet in planetList) {
+ print(planet);
+ }
}
diff --git a/09_while_loop.dart b/09_while_loop.dart
index 9f91313..af187af 100644
--- a/09_while_loop.dart
+++ b/09_while_loop.dart
@@ -1,16 +1,14 @@
-
void main() {
-
- // WHILE Loop
- // WAP to find the even numbers between 1 to 10
-
- var i = 1;
- while (i <= 10) {
-
- if (i % 2 == 0) {
- print(i);
- }
-
- i++;
- }
+ // WHILE Loop
+ // WAP to find the even numbers between 1 to 10
+
+ var i = 0;
+ print('hello ws');
+ while (i <= 500) {
+ print(i);
+ if (i % 2 == 0) {
+ print(i);
+ }
+ i += 18;
+ }
}
diff --git a/11_break_keyword.dart b/11_break_keyword.dart
index ab9b16c..a5c5dd9 100644
--- a/11_break_keyword.dart
+++ b/11_break_keyword.dart
@@ -1,18 +1,17 @@
-
void main() {
+ // BREAK keyword
+ // Using Labels
+ // Nested FOR Loop
- // BREAK keyword
- // Using Labels
- // Nested FOR Loop
-
- myOuterLoop: for (int i = 1; i <= 3; i++) {
-
- innerLoop: for (int j = 1; j <= 3; j++) {
- print("$i $j");
+ myOuterLoop:
+ for (int i = 1; i <= 3; i++) {
+ for (int j = 1; j <= 3; j++) {
+ print("$i $j");
- if (i == 2 && j == 2) {
- break myOuterLoop;
- }
- }
- }
+ if (i == 2 && j == 2) {
+ print('breaking');
+ break myOuterLoop;
+ }
+ }
+ }
}
diff --git a/12_continue_keyword.dart b/12_continue_keyword.dart
index 51fda52..a901b13 100644
--- a/12_continue_keyword.dart
+++ b/12_continue_keyword.dart
@@ -1,17 +1,17 @@
+// ignore_for_file: unused_label
void main() {
+ // CONTINUE keyword
+ // Using Labels
- // CONTINUE keyword
- // Using Labels
-
- myLoop: for (int i = 1; i <= 3; i++) {
-
- myInnerLoop: for (int j = 1; j <= 3; j++) {
-
- if (i == 2 && j == 2) {
- continue myLoop;
- }
- print("$i $j");
- }
- }
+ myLoop:
+ for (int i = 1; i <= 3; i++) {
+ myInnerLoop:
+ for (int j = 1; j <= 3; j++) {
+ if (i == 2 && j == 2) {
+ continue myLoop;
+ }
+ print("$i $j");
+ }
+ }
}
diff --git a/13_functions.dart b/13_functions.dart
index e7c0322..3ebfd0c 100644
--- a/13_functions.dart
+++ b/13_functions.dart
@@ -1,27 +1,21 @@
-
-
// OBJECTIVES
// 1. Define a Function
// 2. Pass parameters to a Function
// 3. Return value from a Function
// 4. Test that by default a Function returns null
-void main() {
-
- findPerimeter(4, 2);
-
- int rectArea = getArea(10, 5);
- print("The area is $rectArea");
-}
-
-void findPerimeter(int length, int breadth) {
-
- int perimeter = 2 * (length + breadth);
- print("The perimeter is $perimeter");
+findPerimeter(int length, int breadth) {
+ int perimeter = 2 * (length + breadth);
+ print("The perimeter is $perimeter");
}
int getArea(int length, int breadth) {
+ int area = length * breadth;
+ return area;
+}
- int area = length * breadth;
- return area;
+void main() {
+ findPerimeter(4, 2);
+ print('o perimetro eh ${findPerimeter(8, 50)}');
+ print("The area is ${getArea(10, 5)}");
}
diff --git a/14_function_expression.dart b/14_function_expression.dart
index 2200ed5..79171aa 100644
--- a/14_function_expression.dart
+++ b/14_function_expression.dart
@@ -1,14 +1,15 @@
// OBJECTIVE: Expression in Function: SHORT HAND SYNTAX
void main() {
+ findPerimeter(4, 2);
- findPerimeter(4, 2);
-
- int rectArea = getArea(10, 5);
- print("The area is $rectArea");
+ int rectArea = getArea(10, 5);
+ print("The area is $rectArea");
+ print('the perimeter is ${findPerimeter(5, 5)}');
}
-void findPerimeter(int length, int breadth) => print("The perimeter is ${2 * (length + breadth)}");
+findPerimeter(int length, int breadth) =>
+ print("The perimeter is ${2 * (length + breadth)}");
int getArea(int length, int breadth) => length * breadth;
diff --git a/15_optional_positional_params.dart b/15_optional_positional_params.dart
index 74d81b8..484c374 100644
--- a/15_optional_positional_params.dart
+++ b/15_optional_positional_params.dart
@@ -1,28 +1,23 @@
-
// 1. Required Parameters
// 2. Optional Positional Parameters
void main() {
+ printCities("New York", "New Delhi", "Sydney");
+ print("");
- printCities("New York", "New Delhi", "Sydney");
- print("");
-
- printCountries("USA"); // You can skip the Optional Positional Parameters
-
+ printCountries("USA"); // You can skip the Optional Positional Parameters
}
// Required Parameters
void printCities(String name1, String name2, String name3) {
-
- print("Name 1 is $name1");
- print("Name 2 is $name2");
- print("Name 3 is $name3");
+ print("Name 1 is $name1");
+ print("Name 2 is $name2");
+ print("Name 3 is $name3");
}
// Optional Positional Parameters
-void printCountries(String name1, [String name2, String name3]) {
-
- print("Name 1 is $name1");
- print("Name 2 is $name2");
- print("Name 3 is $name3");
+void printCountries(String name1, [name2, name3]) {
+ print("Name 1 is $name1");
+ print("Name 2 is $name2");
+ print("Name 3 is $name3");
}
diff --git a/16_named_parameters.dart b/16_named_parameters.dart
index 24dd43e..e48b9d5 100644
--- a/16_named_parameters.dart
+++ b/16_named_parameters.dart
@@ -1,19 +1,17 @@
-
// Optional Named Parameters
void main() {
- findVolume(10, breadth: 5, height: 20);
- print("");
+ findVolume(10, breadth: 5, height: 20);
+ print("");
- findVolume(10, height: 20, breadth: 5); // Sequence doesn't matter in Named Parameter
+ findVolume(10,
+ height: 20, breadth: 5); // Sequence doesn't matter in Named Parameter
}
+void findVolume(int length, {breadth, height}) {
+ print("Length is $length");
+ print("Breadth is $breadth");
+ print("Height is $height");
-int findVolume(int length, {int breadth, int height}) {
-
- print("Length is $length");
- print("Breadth is $breadth");
- print("Height is $height");
-
- print("Volume is ${length * breadth * height}");
+ print("Volume is ${length * breadth * height}");
}
diff --git a/17_default_parameters.dart b/17_default_parameters.dart
index 122f30c..5cfd467 100644
--- a/17_default_parameters.dart
+++ b/17_default_parameters.dart
@@ -1,23 +1,22 @@
-
// Optional Default Parameters
void main() {
+ findVolume(10); // Default value comes into action
+ print("");
- findVolume(10); // Default value comes into action
- print("");
-
- findVolume(10, breadth: 5, height: 30); // Overrides the old value with new one
- print("");
+ findVolume(10,
+ breadth: 5, height: 30); // Overrides the old value with new one
+ print("");
- findVolume(10, height: 30, breadth: 5); // Making use of Named Parameters with Default values
+ findVolume(10,
+ height: 30,
+ breadth: 5); // Making use of Named Parameters with Default values
}
+void findVolume(int length, {int breadth = 2, int height = 20}) {
+ print("Lenght is $length");
+ print("Breadth is $breadth");
+ print("Height is $height");
-int findVolume(int length, {int breadth = 2, int height = 20}) {
-
- print("Lenght is $length");
- print("Breadth is $breadth");
- print("Height is $height");
-
- print("Volume is ${length * breadth * height}");
+ print("Volume is ${length * breadth * height}");
}
diff --git a/18_exception_handling.dart b/18_exception_handling.dart
index 6f3b486..a67afe4 100644
--- a/18_exception_handling.dart
+++ b/18_exception_handling.dart
@@ -1,4 +1,3 @@
-
// OBJECTIVE: Exception Handling
// 1. ON Clause
// 2. Catch Clause with Exception Object
@@ -7,65 +6,68 @@
// 5. Create our own Custom Exception
void main() {
+ print("CASE 1");
+ // CASE 1: When you know the exception to be thrown, use ON Clause
+ try {
+ int result = 12 ~/ 0;
+ print("The result is $result");
+ } on UnsupportedError {
+ print("Cannot divide by Zero");
+ }
- print("CASE 1");
- // CASE 1: When you know the exception to be thrown, use ON Clause
- try {
- int result = 12 ~/ 0;
- print("The result is $result");
- } on IntegerDivisionByZeroException {
- print("Cannot divide by Zero");
- }
-
- print(""); print("CASE 2");
- // CASE 2: When you do not know the exception use CATCH Clause
- try {
- int result = 12 ~/ 0;
- print("The result is $result");
- } catch (e) {
- print("The exception thrown is $e");
- }
+ print("");
+ print("CASE 2");
+ // CASE 2: When you do not know the exception use CATCH Clause
+ try {
+ int result = 12 ~/ 0;
+ print("The result is $result");
+ } catch (e) {
+ print("The exception thrown is $e");
+ }
- print(""); print("CASE 3");
- // CASE 3: Using STACK TRACE to know the events occurred before Exception was thrown
- try {
- int result = 12 ~/ 0;
- print("The result is $result");
- } catch (e, s) {
- print("The exception thrown is $e");
- print("STACK TRACE \n $s");
- }
+ print("");
+ print("CASE 3");
+ // CASE 3: Using STACK TRACE to know the events occurred before Exception was thrown
+ try {
+ int result = 12 ~/ 0;
+ print("The result is $result");
+ } catch (e, s) {
+ print("The exception thrown is $e");
+ print("STACK TRACE \n $s");
+ }
- print(""); print("CASE 4");
- // CASE 4: Whether there is an Exception or not, FINALLY Clause is always Executed
- try {
- int result = 12 ~/ 3;
- print("The result is $result");
- } catch (e) {
- print("The exception thrown is $e");
- } finally {
- print("This is FINALLY Clause and is always executed.");
- }
+ print("");
+ print("CASE 4");
+ // CASE 4: Whether there is an Exception or not, FINALLY Clause is always Executed
+ try {
+ int result = 12 ~/ 3;
+ print("The result is $result");
+ } catch (e) {
+ print("The exception thrown is $e");
+ } finally {
+ print("This is FINALLY Clause and is always executed.");
+ }
- print(""); print("CASE 5");
- // CASE 5: Custom Exception
- try {
- depositMoney(-200);
- } catch (e) {
- print(e.errorMessage());
- } finally {
- // Code
- }
+ print("");
+ print("CASE 5");
+ // CASE 5: Custom Exception
+ try {
+ depositMoney(-200);
+ } catch (e) {
+ print(e);
+ } finally {
+ // Code
+ }
}
class DepositException implements Exception {
- String errorMessage() {
- return "You cannot enter amount less than 0";
- }
+ String errorMessage() {
+ return "You cannot enter amount less than 0";
+ }
}
void depositMoney(int amount) {
- if (amount < 0) {
- throw new DepositException();
- }
+ if (amount < 0) {
+ throw new DepositException();
+ }
}
diff --git a/19_class_and_objects.dart b/19_class_and_objects.dart
index 086ccab..ea62865 100644
--- a/19_class_and_objects.dart
+++ b/19_class_and_objects.dart
@@ -1,31 +1,30 @@
void main() {
+ var student1 = Student(); // One Object, student1 is reference variable
+ student1.id = 23;
+ student1.name = "Peter";
+ print("id ${student1.id} linked to student ${student1.name}");
- var student1 = Student(); // One Object, student1 is reference variable
- student1.id = 23;
- student1.name = "Peter";
- print("${student1.id} and ${student1.name}");
+ student1.study();
+ student1.sleep();
- student1.study();
- student1.sleep();
-
- var student2 = Student(); // One Object, student2 is reference variable
- student2.id = 45;
- student2.name = "Sam";
- print("${student2.id} and ${student2.name}");
- student2.study();
- student2.sleep();
+ var student2 = Student(); // One Object, student2 is reference variable
+ student2.id = 45;
+ student2.name = "Sam";
+ print("${student2.id} and ${student2.name}");
+ student2.study();
+ student2.sleep();
}
// Define states (properties) and behavior of a Student
class Student {
- int id = -1; // Instance or Field Variable, default value is -1
- String name; // Instance or Field Variable, default value is null
+ int? id; // Instance or Field Variable, default value is -1
+ String? name; // Instance or Field Variable, default value is null
- void study() {
- print("${this.name} is now studying");
- }
+ void study() {
+ print("${this.name} is now studying");
+ }
- void sleep() {
- print("${this.name} is now sleeping");
- }
+ void sleep() {
+ print("${this.name} is now sleeping");
+ }
}
diff --git a/20_constructors.dart b/20_constructors.dart
index d39ac4a..111a886 100644
--- a/20_constructors.dart
+++ b/20_constructors.dart
@@ -1,4 +1,3 @@
-
// Objectives
// 1. Default Constructor
// 2. Parameterized Constructor
@@ -6,48 +5,54 @@
// 4. Constant Constructor
void main() {
+ var student1 =
+ Student(23, "Peter"); // One Object, student1 is reference variable
+ print("${student1.id} and ${student1.name}");
- var student1 = Student(23, "Peter"); // One Object, student1 is reference variable
- print("${student1.id} and ${student1.name}");
-
- student1.study();
- student1.sleep();
-
- var student2 = Student(45, "Sam"); // One Object, student2 is reference variable
- print("${student2.id} and ${student2.name}");
+ student1.study();
+ student1.sleep();
- student2.study();
- student2.sleep();
+ var student2 =
+ Student(45, "Sam"); // One Object, student2 is reference variable
+ print("${student2.id} and ${student2.name}");
+ student2.study();
+ student2.sleep();
- var student3 = Student.myCustomConstructor(); // One object, student3 is a reference variable
- student3.id = 54;
- student3.name = "Rahul";
- print("${student3.id} and ${student3.name}");
+ var student3 = Student
+ .myCustomConstructor(); // One object, student3 is a reference variable
+ student3.id = 54;
+ student3.name = "Rahul";
+ print("${student3.id} and ${student3.name}");
+ var student4 = Student.myAnotherNamedConstructor(87, "Paul");
+ print("${student4.id} and ${student4.name}");
- var student4 = Student.myAnotherNamedConstructor(87, "Paul");
- print("${student4.id} and ${student4.name}");
+ var student5 = Student.myCustomConstructor();
+ print("${student5.id} and ${student5.name}");
}
// Define states (properties) and behavior of a Student
class Student {
- int id = -1;
- String name;
+ int id = -1;
+ String? name = 'NonName';
- Student(this.id, this.name); // Parameterised Constructor
+ Student(this.id, this.name); // Parameterised Constructor
- Student.myCustomConstructor() { // Named Constructor
- print("This is my custom constructor");
- }
+ Student.myCustomConstructor() {
+ // Named Constructor
+ this.id;
+ this.name;
+ print("This is my custom constructor");
+ }
- Student.myAnotherNamedConstructor(this.id, this.name); // Named Constructor
+ Student.myAnotherNamedConstructor(this.id, this.name); // Named Constructor
- void study() {
- print("${this.name} is now studying");
- }
+ void study() {
+ print("${this.name} is now studying");
+ }
- void sleep() {
- print("${this.name} is now sleeping");
- }
+ void sleep() {
+ print("${this.name} is now sleeping");
+ }
}
diff --git a/21_getters_setters.dart b/21_getters_setters.dart
index 1d28de5..6c0864b 100644
--- a/21_getters_setters.dart
+++ b/21_getters_setters.dart
@@ -1,27 +1,24 @@
-
// Objectives
// 1. Default Getter and Setter
// 2. Custom Getter and Setter
// 3. Private Instance Variable
void main() {
-
var student = Student();
- student.name = "Peter"; // Calling default Setter to set value
- print(student.name); // Calling default Getter to get value
+ student.name = "Peter"; // Calling default Setter to set value
+ print(student.name); // Calling default Getter to get value
- student.percentage = 438.0; // Calling Custom Setter to set value
- print(student.percentage); // Calling Custom Getter to get value
+ student.percentage = 438.0; // Calling Custom Setter to set value
+ print(student.percentage); // Calling Custom Getter to get value
}
class Student {
+ String? name; // Instance Variable with default Getter and Setter
- String name; // Instance Variable with default Getter and Setter
-
- double _percent; // Private Instance Variable for its own library
+ late double _percent; // Private Instance Variable for its own library
// Instance variable with Custom Setter
- void set percentage(double marksSecured) => _percent = (marksSecured / 500) * 100;
+ void set percentage(double insertedNumber) => _percent = (insertedNumber * 2);
// Instance variable with Custom Getter
double get percentage => _percent;
}
diff --git a/22_inheritance.dart b/22_inheritance.dart
index d1ed8f0..73c0b2c 100644
--- a/22_inheritance.dart
+++ b/22_inheritance.dart
@@ -1,49 +1,48 @@
-
// Objectives
// 1. Exploring Inheritance
void main() {
-
- var dog = Dog();
- dog.breed = "Labrador";
- dog.color = "Black";
- dog.bark();
- dog.eat();
-
- var cat = Cat();
- cat.color = "White";
- cat.age = 6;
- cat.eat();
- cat.meow();
-
- var animal = Animal();
- animal.color = "brown";
- animal.eat();
+ var dog = Dog();
+ dog.breed = "Labrador";
+ dog.color = "Black";
+ dog.bark();
+ dog.eat();
+
+ var cat = Cat();
+ cat.color = "White";
+ cat.age = 6;
+ cat.eat();
+ cat.meow();
+
+ var animal = Animal();
+ animal.color = "brown";
+ animal.eat();
}
class Animal {
+ String? color;
- String color;
-
- void eat() {
- print("Eat !");
- }
+ void eat() {
+ print("Eat !");
+ }
}
-class Dog extends Animal { // Dog is Child class or sub class, Animal is super or parent class
+class Dog extends Animal {
+ // Dog is Child class or sub class, Animal is super or parent class
- String breed;
+ String? breed;
- void bark() {
- print("Bark !");
- }
+ void bark() {
+ print("Bark !");
+ }
}
-class Cat extends Animal { // Cat is Child class or sub class, Animal is super or parent class
+class Cat extends Animal {
+ // Cat is Child class or sub class, Animal is super or parent class
- int age;
+ int? age = 0;
- void meow() {
- print("Meow !");
- }
+ void meow() {
+ print("Meow !");
+ }
}
diff --git a/23_method_overriding.dart b/23_method_overriding.dart
index 99011d6..b5c6beb 100644
--- a/23_method_overriding.dart
+++ b/23_method_overriding.dart
@@ -1,38 +1,36 @@
-
// Objectives
// 1. Exploring Method Overriding
void main() {
-
- var dog = Dog();
- dog.eat();
-
- print(dog.color);
+ var dog = Dog('mastiff', 'yellow');
+ dog.eat();
+ dog.bark();
}
class Animal {
+ String color = "brown";
- String color = "brown";
-
- void eat() {
- print("Animal is eating !");
- }
+ void eat() {
+ print("Animal $color is eating !");
+ }
}
class Dog extends Animal {
-
- String breed;
-
- String color = "Black"; // Property Overriding
-
- void bark() {
- print("Bark !");
- }
-
- // Method Overriding
- void eat() {
- print("Dog is eating !");
- super.eat();
- print("More food to eat");
- }
+ String color = "Black"; // Property Overriding
+ String? breed;
+
+ Dog(this.breed, this.color); // constructor
+
+ void bark() {
+ print("Bark !");
+ }
+
+ // Method Overriding
+ @override
+ void eat() {
+ // overrides Animal 'eat' function
+ print("Dog breed $breed color $color eating !");
+ super.eat();
+ print("More food to eat");
+ }
}
diff --git a/24_inheritance_with_constructors.dart b/24_inheritance_with_constructors.dart
index deae98a..3481eb1 100644
--- a/24_inheritance_with_constructors.dart
+++ b/24_inheritance_with_constructors.dart
@@ -1,46 +1,44 @@
-
// Objectives
// 1. Inheritance with Default Constructor and Parameterised Constructor
// 2. Inheritance with Named Constructor
-void main() {
+// ignore_for_file: unused_local_variable
- var dog1 = Dog("Labrador", "Black");
+void main() {
+ var dog1 = Dog("Labrador", "Black");
- print("");
+ print("");
- var dog2 = Dog("Pug", "Brown");
+ var dog2 = Dog("Pug", "Brown");
- print("");
+ print("");
- var dog3 = Dog.myNamedConstructor("German Shepherd", "Black-Brown");
+ var dog3 = Dog.myNamedConstructor("German Shepherd", "Black-Brown");
}
class Animal {
+ String color;
- String color;
-
- Animal(String color) {
- this.color = color;
- print("Animal class constructor");
- }
+ Animal(this.color) {
+ print("Animal class constructor");
+ }
- Animal.myAnimalNamedConstrctor(String color) {
- print("Animal class named constructor");
- }
+ Animal.myAnimalNamedConstructor(this.color) {
+ print("Animal class named constructor $color");
+ }
}
class Dog extends Animal {
-
- String breed;
-
- Dog(String breed, String color) : super(color) {
- this.breed = breed;
- print("Dog class constructor");
- }
-
- Dog.myNamedConstructor(String breed, String color) : super.myAnimalNamedConstrctor(color) {
- this.breed = breed;
- print("Dog class Named Constructor");
- }
+ String breed;
+
+ Dog(this.breed, String color) : super(color) {
+ this.breed = breed;
+ print("Dog $breed class $color constructor");
+ }
+
+ Dog.myNamedConstructor(this.breed, String color)
+ : super.myAnimalNamedConstructor(color) {
+ this.breed = breed;
+ print("Dog $color class $breed Named Constructor");
+ }
}
diff --git a/25_abstract_class_method.dart b/25_abstract_class_method.dart
index 181a2f3..3dc406c 100644
--- a/25_abstract_class_method.dart
+++ b/25_abstract_class_method.dart
@@ -1,43 +1,37 @@
-
// Objectives
// 1. Abstract Method
// 2. Abstract Class
void main() {
-
// var shape = Shape(); // Error. Cannot instantiate Abstract Class
- var rectangle = Rectangle();
- rectangle.draw();
+ var rectangle = Rectangle();
+ rectangle.draw();
- var circle = Circle();
- circle.draw();
+ var circle = Circle();
+ circle.draw();
}
abstract class Shape {
+ // Define your Instance variable if needed
+ int x = 0;
+ int y = 0;
- // Define your Instance variable if needed
- int x;
- int y;
-
- void draw(); // Abstract Method
+ void draw(); // Abstract Method
- void myNormalFunction() {
- // Some code
- }
+ void myNormalFunction() {
+ // Some code
+ }
}
-
class Rectangle extends Shape {
-
- void draw() {
- print("Drawing Rectangle.....");
- }
+ void draw() {
+ print("Drawing Rectangle.....");
+ }
}
class Circle extends Shape {
-
- void draw() {
- print("Drawing Circle.....");
- }
+ void draw() {
+ print("Drawing Circle.....");
+ }
}
diff --git a/26_interface.dart b/26_interface.dart
index 2a2df42..673284c 100644
--- a/26_interface.dart
+++ b/26_interface.dart
@@ -1,46 +1,41 @@
-
// Objectives
// 1. Interface
void main() {
-
- var tv = Television();
- tv.volumeUp();
- tv.volumeDown();
+ var tv = Television();
+ tv.volumeUp();
+ tv.volumeDown();
+ tv.justAnotherMethod();
}
-class Remote {
-
- void volumeUp() {
- print("______Volume Up from Remote_______");
- }
+abstract class Remote {
+ void volumeUp() {
+ print("______Volume Up from Remote_______");
+ }
- void volumeDown() {
- print("______Volume Down from Remote_______");
- }
+ void volumeDown() {
+ print("______Volume Down from Remote_______");
+ }
}
class AnotherClass {
-
- void justAnotherMethod(){
- // Code
- }
-
+ void justAnotherMethod() {
+ // Code
+ }
}
// Here Remote and AnotherClass acts as Interface
class Television implements Remote, AnotherClass {
-
- void volumeUp() {
+ void volumeUp() {
// super.volumeUp(); // Not allowed to call super while implementing a class as Interface
- print("______Volume Up in Television_______");
- }
+ print("______Volume Up in Television_______");
+ }
- void volumeDown() {
- print("______Volume Down in Television_______");
- }
+ void volumeDown() {
+ print("______Volume Down in Television_______");
+ }
- void justAnotherMethod() {
- print("Some code");
- }
+ void justAnotherMethod() {
+ print("Some code");
+ }
}
diff --git a/27_static_method_variable.dart b/27_static_method_variable.dart
index 0d3f2ce..b856c1b 100644
--- a/27_static_method_variable.dart
+++ b/27_static_method_variable.dart
@@ -1,20 +1,19 @@
-
// Objectives
// 1. Static Methods and Variables
-void main() {
+// ignore_for_file: unused_local_variable
- var circle1 = Circle();
+void main() {
+ var circle1 = Circle();
// circle1.pi; // 4 bytes
- var circle2 = Circle();
+ var circle2 = Circle();
// circle2.pi; // 4 bytes
- // 8 bytes // waste of extra 4 bytes
-
- Circle.pi; // 4 bytes
- Circle.pi; // No more memory will be allocated .
+ // 8 bytes // waste of extra 4 bytes
+ Circle.pi; // 4 bytes
+ Circle.pi; // No more memory will be allocated .
// circle.calculateArea();
@@ -24,22 +23,21 @@ void main() {
}
class Circle {
+ static const double pi = 3.14;
+ static int maxRadius = 5;
- static const double pi = 3.14;
- static int maxRadius = 5;
-
- String color;
+ String? color;
- static void calculateArea() {
- print("Some code to calculate area of Circle");
+ static void calculateArea() {
+ print("Some code to calculate area of Circle");
// myNormalFunction(); // Not allowed to call instance functions
// this.color; // You cannot use 'this' keyword and even cannot access Instance variables
- }
-
- void myNormalFunction() {
- calculateArea();
- this.color = "Red";
- print(pi);
- print(maxRadius);
- }
+ }
+
+ void myNormalFunction() {
+ calculateArea();
+ this.color = "Red";
+ print(pi);
+ print(maxRadius);
+ }
}
diff --git a/31_list_fixed_length.dart b/31_list_fixed_length.dart
index 3dd9581..b6b5191 100644
--- a/31_list_fixed_length.dart
+++ b/31_list_fixed_length.dart
@@ -1,40 +1,40 @@
-
// Objectives
// 1. Fixed-length list
void main() {
+ // Elements: N N N N N
+ // Index: 0 1 2 3 4
- // Elements: N N N N N
- // Index: 0 1 2 3 4
-
- List numbersList = List(5); // Fixed-length list
- numbersList[0] = 73; // Insert operation
- numbersList[1] = 64;
- numbersList[3] = 21;
- numbersList[4] = 12;
+ List numbersList = List.empty(); // Fixed-length list
+ numbersList[0] = 73; // Insert operation
+ numbersList[1] = 64;
+ numbersList[3] = 21;
+ numbersList[4] = 12;
- numbersList[0] = 99; // Update operation
- numbersList[1] = null;// Delete operation
+ numbersList[0] = 99; // Update operation
+ numbersList[1] = null; // Delete operation
- print(numbersList[0]);
- print("\n");
+ print(numbersList[0]);
+ print("\n");
// numbersList.remove(73); // Not supported in fixed-length list
// numbersList.add(24); // Not supported in fixed-length list
// numbersList.removeAt(3); // Not supported in fixed-length list
// numbersList.clear(); // Not supported in fixed-length list
- for (int element in numbersList) { // Using Individual Element (Objects)
- print(element);
- }
+ for (int? element in numbersList) {
+ // Using Individual Element (Objects)
+ print(element);
+ }
- print("\n");
+ print("\n");
- numbersList.forEach((element) => print(element)); // Using Lambda
+ numbersList.forEach((element) => print(element)); // Using Lambda
- print("\n");
+ print("\n");
- for (int i = 0; i < numbersList.length; i++) { // Using Index
- print(numbersList[i]);
- }
+ for (int i = 0; i < numbersList.length; i++) {
+ // Using Index
+ print(numbersList[i]);
+ }
}
diff --git a/32_list_growable.dart b/32_list_growable.dart
index f52a009..507322a 100644
--- a/32_list_growable.dart
+++ b/32_list_growable.dart
@@ -1,46 +1,49 @@
-
// Objectives
// 1. Growable list
void main() {
- // Elements: N 21 12
- // Index: 0 1 2
-
- List countries = ["USA", "INDIA", "CHINA"]; // Growable List : METHOD 1
- countries.add("Nepal");
- countries.add("Japan");
-
-
- List numbersList = List(); // Growable List: METHOD 2
- numbersList.add(73); // Insert Operation
- numbersList.add(64);
- numbersList.add(21);
- numbersList.add(12);
-
- numbersList[0] = 99; // Update operation
- numbersList[1] = null; // Delete operation
-
- print(numbersList[0]);
-
- numbersList.remove(99);
- numbersList.add(24);
- numbersList.removeAt(3);
+ // Elements: N 21 12
+ // Index: 0 1 2
+
+ List countries = [
+ "USA",
+ "INDIA",
+ "CHINA"
+ ]; // Growable List : METHOD 1
+ countries.add("Nepal");
+ countries.add("Japan");
+
+ List numbersList = List.empty(); // Growable List: METHOD 2
+ numbersList.add(73); // Insert Operation
+ numbersList.add(64);
+ numbersList.add(21);
+ numbersList.add(12);
+
+ numbersList[0] = 99; // Update operation
+ numbersList[1] = null; // Delete operation
+
+ print(numbersList[0]);
+
+ numbersList.remove(99);
+ numbersList.add(24);
+ numbersList.removeAt(3);
// numbersList.clear();
- print("\n");
-
- for (int element in numbersList) { // Using Individual Element ( Objects )
- print(element);
- }
+ print("\n");
- print("\n");
+ for (int? element in numbersList) {
+ // Using Individual Element ( Objects )
+ print(element);
+ }
- numbersList.forEach((element) => print(element)); // Using Lambda
+ print("\n");
- print("\n");
+ numbersList.forEach((element) => print(element)); // Using Lambda
- for (int i = 0; i < numbersList.length; i++) { // Using Index
- print(numbersList[i]);
- }
+ print("\n");
+ for (int i = 0; i < numbersList.length; i++) {
+ // Using Index
+ print(numbersList[i]);
+ }
}
diff --git a/33_maps_and_hashmap.dart b/33_maps_and_hashmap.dart
index eaf15ba..71272a6 100644
--- a/33_maps_and_hashmap.dart
+++ b/33_maps_and_hashmap.dart
@@ -1,46 +1,49 @@
-
// Objectives
// 1. Maps
// --> KEY has to be unique
// --> VALUE can be duplicate
-void main() {
-
- Map countryDialingCode = { // Method 1: Using Literal
- "USA": 1,
- "INDIA": 91,
- "PAKISTAN": 92
- };
-
+// ignore_for_file: unused_local_variable
- Map fruits = Map(); // Method 2: Using Constructor
- fruits["apple"] = "red";
- fruits["banana"] = "yellow";
- fruits["guava"] = "green";
-
- fruits.containsKey("apple"); // returns true if the KEY is present in Map
- fruits.update("apple", (value) => "green"); // Update the VALUE for the given KEY
- fruits.remove("apple"); // removes KEY and it's VALUE and returns the VALUE
- fruits.isEmpty; // returns true if the Map is empty
- fruits.length; // returns number of elements in Map
+void main() {
+ Map countryDialingCode = {
+ // Method 1: Using Literal
+ "USA": 1,
+ "INDIA": 91,
+ "PAKISTAN": 92
+ };
+
+ Map fruits = Map(); // Method 2: Using Constructor
+ fruits["apple"] = "red";
+ fruits["banana"] = "yellow";
+ fruits["guava"] = "green";
+
+ fruits.containsKey("apple"); // returns true if the KEY is present in Map
+ fruits.update(
+ "apple", (value) => "green"); // Update the VALUE for the given KEY
+ fruits.remove("apple"); // removes KEY and it's VALUE and returns the VALUE
+ fruits.isEmpty; // returns true if the Map is empty
+ fruits.length; // returns number of elements in Map
// fruits.clear(); // Deletes all elements
- print(fruits["apple"]);
-
- print("\n");
+ print(fruits["apple"]);
- for (String key in fruits.keys) { // Print all keys
- print(key);
- }
+ print("\n");
- print("\n");
+ for (String key in fruits.keys) {
+ // Print all keys
+ print(key);
+ }
- for (String value in fruits.values) { // Print all values
- print(value);
- }
+ print("\n");
- print("\n");
+ for (String value in fruits.values) {
+ // Print all values
+ print(value);
+ }
- fruits.forEach((key, value) => print("key: $key and value: $value")); // Using Lambda
+ print("\n");
+ fruits.forEach(
+ (key, value) => print("key: $key and value: $value")); // Using Lambda
}