├── .idea ├── codeStyles │ └── codeStyleConfig.xml ├── libraries │ └── Dart_SDK.xml ├── misc.xml ├── modules.xml └── vcs.xml ├── 01_hello_world.dart ├── 02_built_in_data_types.dart ├── 03_string_and_string_interpolation.dart ├── 04_constants.dart ├── 05_if_else_control_flow.dart ├── 06_conditional_expressions.dart ├── 07_switch_and_case.dart ├── 08_for_loop.dart ├── 09_while_loop.dart ├── 10_do_while_loop.dart ├── 11_break_keyword.dart ├── 12_continue_keyword.dart ├── 13_functions.dart ├── 14_function_expression.dart ├── 15_optional_positional_params.dart ├── 16_named_parameters.dart ├── 17_default_parameters.dart ├── 18_exception_handling.dart ├── 19_class_and_objects.dart ├── 20_constructors.dart ├── 21_getters_setters.dart ├── 22_inheritance.dart ├── 23_method_overriding.dart ├── 24_inheritance_with_constructors.dart ├── 25_abstract_class_method.dart ├── 26_interface.dart ├── 27_static_method_variable.dart ├── 28_lambda_nameless_function.dart ├── 29_higher_order_functions.dart ├── 30_lexical_closures.dart ├── 31_list_fixed_length.dart ├── 32_list_growable.dart ├── 33_maps_and_hashmap.dart ├── 34_set_and_hashset.dart ├── 35_callable_classes.dart ├── DartFundamentals.iml └── README.md /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/libraries/Dart_SDK.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /01_hello_world.dart: -------------------------------------------------------------------------------- 1 | 2 | 3 | void main() { 4 | 5 | // This is my first line of code 6 | print("Hello World"); // this is another comment .... 7 | 8 | print("This is my first application"); 9 | 10 | // Performing arithematic operation 11 | print(12 / 4); 12 | 13 | // Printing out boolean value 14 | print(false); 15 | } 16 | -------------------------------------------------------------------------------- /02_built_in_data_types.dart: -------------------------------------------------------------------------------- 1 | 2 | void main(List arguments) { 3 | 4 | // Numbers: int 5 | int score = 23; 6 | var count = 23; // It is inferred as integer automatically by compiler 7 | int hexValue = 0xEADEBAEE; 8 | 9 | // Numbers: double 10 | double percentage = 93.4; 11 | var percent = 82.533; 12 | double exponents = 1.42e5; 13 | 14 | // Strings 15 | String name = "Henry"; 16 | var company = "Google"; 17 | 18 | // Boolean 19 | bool isValid = true; 20 | var isAlive = false; 21 | 22 | print(score); 23 | print(exponents); 24 | 25 | // NOTE: All data types in Dart are Objects. 26 | // Therefore, their initial value is by default 'null' 27 | } 28 | -------------------------------------------------------------------------------- /03_string_and_string_interpolation.dart: -------------------------------------------------------------------------------- 1 | 2 | void main() { 3 | 4 | // Literals 5 | var isCool = true; 6 | int x = 2; 7 | "John"; 8 | 4.5; 9 | 10 | // Various ways to define String Literals in Dart 11 | String s1 = 'Single'; 12 | String s2 = "Double"; 13 | String s3 = 'It\'s easy'; 14 | String s4 = "It's easy"; 15 | 16 | String s5 = 'This is going to be a very long String. ' 17 | 'This is just a sample String demo in Dart Programming Language'; 18 | 19 | 20 | // String Interpolation : Use ["My name is $name"] instead of ["My name is " + name] 21 | String name = "Kevin"; 22 | 23 | print("My name is $name"); 24 | print("The number of characters in String Kevin is ${name.length}"); 25 | 26 | 27 | int l = 20; 28 | int b = 10; 29 | 30 | print("The sum of $l and $b is ${l + b}"); 31 | print("The area of rectangle with length $l and breadth $b is ${l * b}"); 32 | } 33 | -------------------------------------------------------------------------------- /04_constants.dart: -------------------------------------------------------------------------------- 1 | 2 | void main() { 3 | 4 | // final 5 | final cityName = 'Mumbai'; 6 | // name = 'Peter'; // Throws an error 7 | 8 | final String countryName = 'India'; 9 | 10 | // const 11 | const PI = 3.14; 12 | const double gravity = 9.8; 13 | } 14 | 15 | class Circle { 16 | 17 | final color = 'Red'; 18 | static const PI = 3.14; 19 | } 20 | -------------------------------------------------------------------------------- /05_if_else_control_flow.dart: -------------------------------------------------------------------------------- 1 | 2 | void main() { 3 | 4 | // IF and ELSE Statements 5 | var salary = 15000; 6 | 7 | if (salary > 20000) { 8 | print("You got promotion. Congratulations !"); 9 | } else { 10 | print("You need to work hard !"); 11 | } 12 | 13 | // IF ELSE IF Ladder statements 14 | var marks = 70; 15 | 16 | if (marks >= 90 && marks < 100) { 17 | print("A+ grade"); 18 | } else if (marks >= 80 && marks < 90) { 19 | print("A grade"); 20 | } else if (marks >= 70 && marks < 80) { 21 | print("B grade"); 22 | } else if (marks >= 60 && marks < 70) { 23 | print("C grade"); 24 | } else if (marks > 30 && marks < 60) { 25 | print("D grade"); 26 | } else if (marks >= 0 && marks < 30) { 27 | print("You have failed"); 28 | } else { 29 | print("Invalid Marks. Please try again !"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /06_conditional_expressions.dart: -------------------------------------------------------------------------------- 1 | 2 | void main() { 3 | 4 | // Conditional Expressions 5 | 6 | // 1. condition ? exp1 : exp2 7 | // If condition is true, evaluates expr1 (and returns its value); 8 | // otherwise, evaluates and returns the value of expr2. 9 | 10 | int a = 2; 11 | int b = 3; 12 | 13 | int smallNumber = a < b ? a : b; 14 | print("$smallNumber is smaller"); 15 | 16 | 17 | 18 | // 2. exp1 ?? exp2 19 | // If expr1 is non-null, returns its value; otherwise, evaluates and 20 | // returns the value of expr2. 21 | 22 | String name = null; 23 | 24 | String nameToPrint = name ?? "Guest User"; 25 | print(nameToPrint); 26 | } 27 | -------------------------------------------------------------------------------- /07_switch_and_case.dart: -------------------------------------------------------------------------------- 1 | 2 | void main() { 3 | 4 | // Switch Case Statements: Applicable for only 'int' and 'String' 5 | 6 | String grade = 'A'; 7 | 8 | switch (grade) { 9 | 10 | case 'A': 11 | print("Excellent grade of A"); 12 | break; 13 | 14 | case 'B': 15 | print("Very Good !"); 16 | break; 17 | 18 | case 'C': 19 | print("Good enough. But work hard"); 20 | break; 21 | 22 | case 'F': 23 | print("You have failed"); 24 | break; 25 | default: 26 | print("Invalid Grade"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /08_for_loop.dart: -------------------------------------------------------------------------------- 1 | 2 | void main() { 3 | 4 | // FOR Loop 5 | 6 | // WAP to find the even numbers between 1 to 10 7 | 8 | for (int i = 1; i <= 10; i++) { 9 | 10 | if ( i % 2 == 0) { 11 | print(i); 12 | } 13 | } 14 | 15 | 16 | // for ..in loop 17 | List planetList = ["Mercury", "Venus", "Earth", "Mars"]; 18 | 19 | for (String planet in planetList) { 20 | print(planet); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /09_while_loop.dart: -------------------------------------------------------------------------------- 1 | 2 | void main() { 3 | 4 | // WHILE Loop 5 | // WAP to find the even numbers between 1 to 10 6 | 7 | var i = 1; 8 | while (i <= 10) { 9 | 10 | if (i % 2 == 0) { 11 | print(i); 12 | } 13 | 14 | i++; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /10_do_while_loop.dart: -------------------------------------------------------------------------------- 1 | 2 | void main() { 3 | 4 | // DO-WHILE Loop 5 | // WAP to find the even numbers between 1 to 10 6 | 7 | int i = 1; 8 | 9 | do { 10 | 11 | if ( i % 2 == 0) { 12 | print(i); 13 | } 14 | 15 | i++; 16 | } while ( i <= 10); 17 | } 18 | -------------------------------------------------------------------------------- /11_break_keyword.dart: -------------------------------------------------------------------------------- 1 | 2 | void main() { 3 | 4 | // BREAK keyword 5 | // Using Labels 6 | // Nested FOR Loop 7 | 8 | myOuterLoop: for (int i = 1; i <= 3; i++) { 9 | 10 | innerLoop: for (int j = 1; j <= 3; j++) { 11 | print("$i $j"); 12 | 13 | if (i == 2 && j == 2) { 14 | break myOuterLoop; 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /12_continue_keyword.dart: -------------------------------------------------------------------------------- 1 | 2 | void main() { 3 | 4 | // CONTINUE keyword 5 | // Using Labels 6 | 7 | myLoop: for (int i = 1; i <= 3; i++) { 8 | 9 | myInnerLoop: for (int j = 1; j <= 3; j++) { 10 | 11 | if (i == 2 && j == 2) { 12 | continue myLoop; 13 | } 14 | print("$i $j"); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /13_functions.dart: -------------------------------------------------------------------------------- 1 | 2 | 3 | // OBJECTIVES 4 | // 1. Define a Function 5 | // 2. Pass parameters to a Function 6 | // 3. Return value from a Function 7 | // 4. Test that by default a Function returns null 8 | 9 | void main() { 10 | 11 | findPerimeter(4, 2); 12 | 13 | int rectArea = getArea(10, 5); 14 | print("The area is $rectArea"); 15 | } 16 | 17 | void findPerimeter(int length, int breadth) { 18 | 19 | int perimeter = 2 * (length + breadth); 20 | print("The perimeter is $perimeter"); 21 | } 22 | 23 | int getArea(int length, int breadth) { 24 | 25 | int area = length * breadth; 26 | return area; 27 | } 28 | -------------------------------------------------------------------------------- /14_function_expression.dart: -------------------------------------------------------------------------------- 1 | // OBJECTIVE: Expression in Function: SHORT HAND SYNTAX 2 | 3 | void main() { 4 | 5 | findPerimeter(4, 2); 6 | 7 | int rectArea = getArea(10, 5); 8 | print("The area is $rectArea"); 9 | } 10 | 11 | void findPerimeter(int length, int breadth) => print("The perimeter is ${2 * (length + breadth)}"); 12 | 13 | int getArea(int length, int breadth) => length * breadth; 14 | 15 | 16 | // "=>" is known as FAT ARROW 17 | // "=> expression" is a SHORT HAND SYNTAX for { return expression; } 18 | // Example "=> length * breadth" is SHORT HAND SYNTAX for { return length * breadth; } 19 | -------------------------------------------------------------------------------- /15_optional_positional_params.dart: -------------------------------------------------------------------------------- 1 | 2 | // 1. Required Parameters 3 | // 2. Optional Positional Parameters 4 | 5 | void main() { 6 | 7 | printCities("New York", "New Delhi", "Sydney"); 8 | print(""); 9 | 10 | printCountries("USA"); // You can skip the Optional Positional Parameters 11 | 12 | } 13 | 14 | // Required Parameters 15 | void printCities(String name1, String name2, String name3) { 16 | 17 | print("Name 1 is $name1"); 18 | print("Name 2 is $name2"); 19 | print("Name 3 is $name3"); 20 | } 21 | 22 | // Optional Positional Parameters 23 | void printCountries(String name1, [String name2, String name3]) { 24 | 25 | print("Name 1 is $name1"); 26 | print("Name 2 is $name2"); 27 | print("Name 3 is $name3"); 28 | } 29 | -------------------------------------------------------------------------------- /16_named_parameters.dart: -------------------------------------------------------------------------------- 1 | 2 | // Optional Named Parameters 3 | 4 | void main() { 5 | findVolume(10, breadth: 5, height: 20); 6 | print(""); 7 | 8 | findVolume(10, height: 20, breadth: 5); // Sequence doesn't matter in Named Parameter 9 | } 10 | 11 | 12 | int findVolume(int length, {int breadth, int height}) { 13 | 14 | print("Length is $length"); 15 | print("Breadth is $breadth"); 16 | print("Height is $height"); 17 | 18 | print("Volume is ${length * breadth * height}"); 19 | } 20 | -------------------------------------------------------------------------------- /17_default_parameters.dart: -------------------------------------------------------------------------------- 1 | 2 | // Optional Default Parameters 3 | 4 | void main() { 5 | 6 | findVolume(10); // Default value comes into action 7 | print(""); 8 | 9 | findVolume(10, breadth: 5, height: 30); // Overrides the old value with new one 10 | print(""); 11 | 12 | findVolume(10, height: 30, breadth: 5); // Making use of Named Parameters with Default values 13 | } 14 | 15 | 16 | int findVolume(int length, {int breadth = 2, int height = 20}) { 17 | 18 | print("Lenght is $length"); 19 | print("Breadth is $breadth"); 20 | print("Height is $height"); 21 | 22 | print("Volume is ${length * breadth * height}"); 23 | } 24 | -------------------------------------------------------------------------------- /18_exception_handling.dart: -------------------------------------------------------------------------------- 1 | 2 | // OBJECTIVE: Exception Handling 3 | // 1. ON Clause 4 | // 2. Catch Clause with Exception Object 5 | // 3. Catch Clause with Exception Object and StackTrace Object 6 | // 4. Finally Clause 7 | // 5. Create our own Custom Exception 8 | 9 | void main() { 10 | 11 | print("CASE 1"); 12 | // CASE 1: When you know the exception to be thrown, use ON Clause 13 | try { 14 | int result = 12 ~/ 0; 15 | print("The result is $result"); 16 | } on IntegerDivisionByZeroException { 17 | print("Cannot divide by Zero"); 18 | } 19 | 20 | print(""); print("CASE 2"); 21 | // CASE 2: When you do not know the exception use CATCH Clause 22 | try { 23 | int result = 12 ~/ 0; 24 | print("The result is $result"); 25 | } catch (e) { 26 | print("The exception thrown is $e"); 27 | } 28 | 29 | print(""); print("CASE 3"); 30 | // CASE 3: Using STACK TRACE to know the events occurred before Exception was thrown 31 | try { 32 | int result = 12 ~/ 0; 33 | print("The result is $result"); 34 | } catch (e, s) { 35 | print("The exception thrown is $e"); 36 | print("STACK TRACE \n $s"); 37 | } 38 | 39 | print(""); print("CASE 4"); 40 | // CASE 4: Whether there is an Exception or not, FINALLY Clause is always Executed 41 | try { 42 | int result = 12 ~/ 3; 43 | print("The result is $result"); 44 | } catch (e) { 45 | print("The exception thrown is $e"); 46 | } finally { 47 | print("This is FINALLY Clause and is always executed."); 48 | } 49 | 50 | print(""); print("CASE 5"); 51 | // CASE 5: Custom Exception 52 | try { 53 | depositMoney(-200); 54 | } catch (e) { 55 | print(e.errorMessage()); 56 | } finally { 57 | // Code 58 | } 59 | } 60 | 61 | class DepositException implements Exception { 62 | String errorMessage() { 63 | return "You cannot enter amount less than 0"; 64 | } 65 | } 66 | 67 | void depositMoney(int amount) { 68 | if (amount < 0) { 69 | throw new DepositException(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /19_class_and_objects.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | 3 | var student1 = Student(); // One Object, student1 is reference variable 4 | student1.id = 23; 5 | student1.name = "Peter"; 6 | print("${student1.id} and ${student1.name}"); 7 | 8 | student1.study(); 9 | student1.sleep(); 10 | 11 | var student2 = Student(); // One Object, student2 is reference variable 12 | student2.id = 45; 13 | student2.name = "Sam"; 14 | print("${student2.id} and ${student2.name}"); 15 | student2.study(); 16 | student2.sleep(); 17 | } 18 | 19 | // Define states (properties) and behavior of a Student 20 | class Student { 21 | int id = -1; // Instance or Field Variable, default value is -1 22 | String name; // Instance or Field Variable, default value is null 23 | 24 | void study() { 25 | print("${this.name} is now studying"); 26 | } 27 | 28 | void sleep() { 29 | print("${this.name} is now sleeping"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /20_constructors.dart: -------------------------------------------------------------------------------- 1 | 2 | // Objectives 3 | // 1. Default Constructor 4 | // 2. Parameterized Constructor 5 | // 3. Named Constructor 6 | // 4. Constant Constructor 7 | 8 | void main() { 9 | 10 | var student1 = Student(23, "Peter"); // One Object, student1 is reference variable 11 | print("${student1.id} and ${student1.name}"); 12 | 13 | student1.study(); 14 | student1.sleep(); 15 | 16 | var student2 = Student(45, "Sam"); // One Object, student2 is reference variable 17 | print("${student2.id} and ${student2.name}"); 18 | 19 | student2.study(); 20 | student2.sleep(); 21 | 22 | 23 | var student3 = Student.myCustomConstructor(); // One object, student3 is a reference variable 24 | student3.id = 54; 25 | student3.name = "Rahul"; 26 | print("${student3.id} and ${student3.name}"); 27 | 28 | 29 | var student4 = Student.myAnotherNamedConstructor(87, "Paul"); 30 | print("${student4.id} and ${student4.name}"); 31 | } 32 | 33 | // Define states (properties) and behavior of a Student 34 | class Student { 35 | int id = -1; 36 | String name; 37 | 38 | Student(this.id, this.name); // Parameterised Constructor 39 | 40 | Student.myCustomConstructor() { // Named Constructor 41 | print("This is my custom constructor"); 42 | } 43 | 44 | Student.myAnotherNamedConstructor(this.id, this.name); // Named Constructor 45 | 46 | void study() { 47 | print("${this.name} is now studying"); 48 | } 49 | 50 | void sleep() { 51 | print("${this.name} is now sleeping"); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /21_getters_setters.dart: -------------------------------------------------------------------------------- 1 | 2 | // Objectives 3 | // 1. Default Getter and Setter 4 | // 2. Custom Getter and Setter 5 | // 3. Private Instance Variable 6 | 7 | void main() { 8 | 9 | var student = Student(); 10 | student.name = "Peter"; // Calling default Setter to set value 11 | print(student.name); // Calling default Getter to get value 12 | 13 | student.percentage = 438.0; // Calling Custom Setter to set value 14 | print(student.percentage); // Calling Custom Getter to get value 15 | } 16 | 17 | class Student { 18 | 19 | String name; // Instance Variable with default Getter and Setter 20 | 21 | double _percent; // Private Instance Variable for its own library 22 | 23 | // Instance variable with Custom Setter 24 | void set percentage(double marksSecured) => _percent = (marksSecured / 500) * 100; 25 | // Instance variable with Custom Getter 26 | double get percentage => _percent; 27 | } 28 | -------------------------------------------------------------------------------- /22_inheritance.dart: -------------------------------------------------------------------------------- 1 | 2 | // Objectives 3 | // 1. Exploring Inheritance 4 | 5 | void main() { 6 | 7 | var dog = Dog(); 8 | dog.breed = "Labrador"; 9 | dog.color = "Black"; 10 | dog.bark(); 11 | dog.eat(); 12 | 13 | var cat = Cat(); 14 | cat.color = "White"; 15 | cat.age = 6; 16 | cat.eat(); 17 | cat.meow(); 18 | 19 | var animal = Animal(); 20 | animal.color = "brown"; 21 | animal.eat(); 22 | } 23 | 24 | class Animal { 25 | 26 | String color; 27 | 28 | void eat() { 29 | print("Eat !"); 30 | } 31 | } 32 | 33 | class Dog extends Animal { // Dog is Child class or sub class, Animal is super or parent class 34 | 35 | String breed; 36 | 37 | void bark() { 38 | print("Bark !"); 39 | } 40 | } 41 | 42 | class Cat extends Animal { // Cat is Child class or sub class, Animal is super or parent class 43 | 44 | int age; 45 | 46 | void meow() { 47 | print("Meow !"); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /23_method_overriding.dart: -------------------------------------------------------------------------------- 1 | 2 | // Objectives 3 | // 1. Exploring Method Overriding 4 | 5 | void main() { 6 | 7 | var dog = Dog(); 8 | dog.eat(); 9 | 10 | print(dog.color); 11 | } 12 | 13 | class Animal { 14 | 15 | String color = "brown"; 16 | 17 | void eat() { 18 | print("Animal is eating !"); 19 | } 20 | } 21 | 22 | class Dog extends Animal { 23 | 24 | String breed; 25 | 26 | String color = "Black"; // Property Overriding 27 | 28 | void bark() { 29 | print("Bark !"); 30 | } 31 | 32 | // Method Overriding 33 | void eat() { 34 | print("Dog is eating !"); 35 | super.eat(); 36 | print("More food to eat"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /24_inheritance_with_constructors.dart: -------------------------------------------------------------------------------- 1 | 2 | // Objectives 3 | // 1. Inheritance with Default Constructor and Parameterised Constructor 4 | // 2. Inheritance with Named Constructor 5 | 6 | void main() { 7 | 8 | var dog1 = Dog("Labrador", "Black"); 9 | 10 | print(""); 11 | 12 | var dog2 = Dog("Pug", "Brown"); 13 | 14 | print(""); 15 | 16 | var dog3 = Dog.myNamedConstructor("German Shepherd", "Black-Brown"); 17 | } 18 | 19 | class Animal { 20 | 21 | String color; 22 | 23 | Animal(String color) { 24 | this.color = color; 25 | print("Animal class constructor"); 26 | } 27 | 28 | Animal.myAnimalNamedConstrctor(String color) { 29 | print("Animal class named constructor"); 30 | } 31 | } 32 | 33 | class Dog extends Animal { 34 | 35 | String breed; 36 | 37 | Dog(String breed, String color) : super(color) { 38 | this.breed = breed; 39 | print("Dog class constructor"); 40 | } 41 | 42 | Dog.myNamedConstructor(String breed, String color) : super.myAnimalNamedConstrctor(color) { 43 | this.breed = breed; 44 | print("Dog class Named Constructor"); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /25_abstract_class_method.dart: -------------------------------------------------------------------------------- 1 | 2 | // Objectives 3 | // 1. Abstract Method 4 | // 2. Abstract Class 5 | 6 | void main() { 7 | 8 | // var shape = Shape(); // Error. Cannot instantiate Abstract Class 9 | 10 | var rectangle = Rectangle(); 11 | rectangle.draw(); 12 | 13 | var circle = Circle(); 14 | circle.draw(); 15 | } 16 | 17 | abstract class Shape { 18 | 19 | // Define your Instance variable if needed 20 | int x; 21 | int y; 22 | 23 | void draw(); // Abstract Method 24 | 25 | void myNormalFunction() { 26 | // Some code 27 | } 28 | } 29 | 30 | 31 | class Rectangle extends Shape { 32 | 33 | void draw() { 34 | print("Drawing Rectangle....."); 35 | } 36 | } 37 | 38 | class Circle extends Shape { 39 | 40 | void draw() { 41 | print("Drawing Circle....."); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /26_interface.dart: -------------------------------------------------------------------------------- 1 | 2 | // Objectives 3 | // 1. Interface 4 | 5 | void main() { 6 | 7 | var tv = Television(); 8 | tv.volumeUp(); 9 | tv.volumeDown(); 10 | } 11 | 12 | class Remote { 13 | 14 | void volumeUp() { 15 | print("______Volume Up from Remote_______"); 16 | } 17 | 18 | void volumeDown() { 19 | print("______Volume Down from Remote_______"); 20 | } 21 | } 22 | 23 | class AnotherClass { 24 | 25 | void justAnotherMethod(){ 26 | // Code 27 | } 28 | 29 | } 30 | 31 | // Here Remote and AnotherClass acts as Interface 32 | class Television implements Remote, AnotherClass { 33 | 34 | void volumeUp() { 35 | // super.volumeUp(); // Not allowed to call super while implementing a class as Interface 36 | print("______Volume Up in Television_______"); 37 | } 38 | 39 | void volumeDown() { 40 | print("______Volume Down in Television_______"); 41 | } 42 | 43 | void justAnotherMethod() { 44 | print("Some code"); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /27_static_method_variable.dart: -------------------------------------------------------------------------------- 1 | 2 | // Objectives 3 | // 1. Static Methods and Variables 4 | 5 | void main() { 6 | 7 | var circle1 = Circle(); 8 | // circle1.pi; // 4 bytes 9 | 10 | var circle2 = Circle(); 11 | // circle2.pi; // 4 bytes 12 | 13 | // 8 bytes // waste of extra 4 bytes 14 | 15 | Circle.pi; // 4 bytes 16 | Circle.pi; // No more memory will be allocated . 17 | 18 | 19 | // circle.calculateArea(); 20 | 21 | // print(Circle.pi); // Syntax to call Static Variable 22 | 23 | // Circle.calculateArea(); // Syntax to call Static Method 24 | } 25 | 26 | class Circle { 27 | 28 | static const double pi = 3.14; 29 | static int maxRadius = 5; 30 | 31 | String color; 32 | 33 | static void calculateArea() { 34 | print("Some code to calculate area of Circle"); 35 | // myNormalFunction(); // Not allowed to call instance functions 36 | // this.color; // You cannot use 'this' keyword and even cannot access Instance variables 37 | } 38 | 39 | void myNormalFunction() { 40 | calculateArea(); 41 | this.color = "Red"; 42 | print(pi); 43 | print(maxRadius); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /28_lambda_nameless_function.dart: -------------------------------------------------------------------------------- 1 | 2 | // Objectives 3 | // 1. Lambda Functions 4 | // NOTE: A function in Dart is object 5 | 6 | void main() { 7 | 8 | // Defining Lambda: 1st way 9 | Function addTwoNumbers = (int a, int b) { 10 | var sum = a + b; 11 | print(sum); 12 | }; 13 | 14 | var multiplyByFour = (int number) { 15 | return number * 4; 16 | }; 17 | 18 | // Defining Lambda: 2nd way: Function Expression: Using Short Hand Syntax or FAT Arrow ( '=>' ) 19 | Function addNumbers = (int a, int b) => print(a + b); 20 | 21 | var multiplyFour = (int number) => number * 4; 22 | 23 | 24 | // Calling lambda function 25 | addTwoNumbers(2, 5); 26 | print(multiplyByFour(5)); 27 | 28 | addNumbers(3, 7); 29 | print(multiplyFour(10)); 30 | } 31 | 32 | 33 | // A example of Normal function 34 | void addMyNumbers(int a, int b) { 35 | 36 | var sum = a + b; 37 | print(sum); 38 | } 39 | -------------------------------------------------------------------------------- /29_higher_order_functions.dart: -------------------------------------------------------------------------------- 1 | 2 | 3 | // Objectives 4 | // 1. Higher-Order Function: 5 | // How to pass function as parameter? 6 | // How to return a function from another function? 7 | 8 | 9 | void main() { 10 | 11 | // Example One: Passing Function to Higher-Order Function 12 | Function addNumbers = (a, b) => print(a + b); 13 | someOtherFunction("Hello", addNumbers); 14 | 15 | 16 | // Example Two: Receiving Function from Higher-Order Function 17 | var myFunc = taskToPerform(); 18 | print(myFunc(10)); // multiplyFour(10) // number * 4 // 10 * 4 // OUTPUT: 40 19 | } 20 | 21 | 22 | 23 | // Example one: Accepts function as parameter 24 | void someOtherFunction(String message, Function myFunction) { // Higher-Order Function 25 | 26 | print(message); 27 | myFunction(2, 4); // addNumbers(2, 4) // print(a + b); // print(2 + 4) // OUTPUT: 6 28 | } 29 | 30 | 31 | // Example two: Returns a function 32 | Function taskToPerform() { // Higher-Order Function 33 | 34 | Function multiplyFour = (int number) => number * 4; 35 | return multiplyFour; 36 | } 37 | -------------------------------------------------------------------------------- /30_lexical_closures.dart: -------------------------------------------------------------------------------- 1 | 2 | 3 | // Objective 4 | // 1. Closures 5 | 6 | 7 | void main() { 8 | 9 | // Definition 1: 10 | // A closure is a function that has access to the parent scope, even after the scope has closed. 11 | 12 | String message = "Dar is good"; 13 | 14 | Function showMessage = () { 15 | message = "Dart is awesome"; 16 | print(message); 17 | }; 18 | 19 | showMessage(); 20 | 21 | 22 | // Definition 2: 23 | // A closure is a function object that has access to variables in its lexical scope, 24 | // even when the function is used outside of its original scope. 25 | 26 | Function talk = () { 27 | 28 | String msg = "Hi"; 29 | 30 | Function say = () { 31 | msg = "Hello"; 32 | print(msg); 33 | }; 34 | 35 | return say; 36 | }; 37 | 38 | Function speak = talk(); 39 | 40 | speak(); // talk() // say() // print(msg) // "Hello" 41 | } 42 | -------------------------------------------------------------------------------- /31_list_fixed_length.dart: -------------------------------------------------------------------------------- 1 | 2 | // Objectives 3 | // 1. Fixed-length list 4 | 5 | void main() { 6 | 7 | // Elements: N N N N N 8 | // Index: 0 1 2 3 4 9 | 10 | List numbersList = List(5); // Fixed-length list 11 | numbersList[0] = 73; // Insert operation 12 | numbersList[1] = 64; 13 | numbersList[3] = 21; 14 | numbersList[4] = 12; 15 | 16 | numbersList[0] = 99; // Update operation 17 | numbersList[1] = null;// Delete operation 18 | 19 | print(numbersList[0]); 20 | print("\n"); 21 | 22 | // numbersList.remove(73); // Not supported in fixed-length list 23 | // numbersList.add(24); // Not supported in fixed-length list 24 | // numbersList.removeAt(3); // Not supported in fixed-length list 25 | // numbersList.clear(); // Not supported in fixed-length list 26 | 27 | for (int element in numbersList) { // Using Individual Element (Objects) 28 | print(element); 29 | } 30 | 31 | print("\n"); 32 | 33 | numbersList.forEach((element) => print(element)); // Using Lambda 34 | 35 | print("\n"); 36 | 37 | for (int i = 0; i < numbersList.length; i++) { // Using Index 38 | print(numbersList[i]); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /32_list_growable.dart: -------------------------------------------------------------------------------- 1 | 2 | // Objectives 3 | // 1. Growable list 4 | 5 | void main() { 6 | // Elements: N 21 12 7 | // Index: 0 1 2 8 | 9 | List countries = ["USA", "INDIA", "CHINA"]; // Growable List : METHOD 1 10 | countries.add("Nepal"); 11 | countries.add("Japan"); 12 | 13 | 14 | List numbersList = List(); // Growable List: METHOD 2 15 | numbersList.add(73); // Insert Operation 16 | numbersList.add(64); 17 | numbersList.add(21); 18 | numbersList.add(12); 19 | 20 | numbersList[0] = 99; // Update operation 21 | numbersList[1] = null; // Delete operation 22 | 23 | print(numbersList[0]); 24 | 25 | numbersList.remove(99); 26 | numbersList.add(24); 27 | numbersList.removeAt(3); 28 | // numbersList.clear(); 29 | 30 | print("\n"); 31 | 32 | for (int element in numbersList) { // Using Individual Element ( Objects ) 33 | print(element); 34 | } 35 | 36 | print("\n"); 37 | 38 | numbersList.forEach((element) => print(element)); // Using Lambda 39 | 40 | print("\n"); 41 | 42 | for (int i = 0; i < numbersList.length; i++) { // Using Index 43 | print(numbersList[i]); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /33_maps_and_hashmap.dart: -------------------------------------------------------------------------------- 1 | 2 | // Objectives 3 | // 1. Maps 4 | // --> KEY has to be unique 5 | // --> VALUE can be duplicate 6 | 7 | void main() { 8 | 9 | Map countryDialingCode = { // Method 1: Using Literal 10 | "USA": 1, 11 | "INDIA": 91, 12 | "PAKISTAN": 92 13 | }; 14 | 15 | 16 | Map fruits = Map(); // Method 2: Using Constructor 17 | fruits["apple"] = "red"; 18 | fruits["banana"] = "yellow"; 19 | fruits["guava"] = "green"; 20 | 21 | fruits.containsKey("apple"); // returns true if the KEY is present in Map 22 | fruits.update("apple", (value) => "green"); // Update the VALUE for the given KEY 23 | fruits.remove("apple"); // removes KEY and it's VALUE and returns the VALUE 24 | fruits.isEmpty; // returns true if the Map is empty 25 | fruits.length; // returns number of elements in Map 26 | // fruits.clear(); // Deletes all elements 27 | 28 | print(fruits["apple"]); 29 | 30 | print("\n"); 31 | 32 | for (String key in fruits.keys) { // Print all keys 33 | print(key); 34 | } 35 | 36 | print("\n"); 37 | 38 | for (String value in fruits.values) { // Print all values 39 | print(value); 40 | } 41 | 42 | print("\n"); 43 | 44 | fruits.forEach((key, value) => print("key: $key and value: $value")); // Using Lambda 45 | 46 | } 47 | -------------------------------------------------------------------------------- /34_set_and_hashset.dart: -------------------------------------------------------------------------------- 1 | 2 | // Objectives 3 | // 1. Sets: 4 | // --> Unordered Collection 5 | // --> All elements are unique 6 | 7 | void main() { 8 | 9 | Set countries = Set.from(["USA", "INDIA", "CHINA"]); // Method 1: From a list 10 | countries.add("Nepal"); 11 | countries.add("Japan"); 12 | 13 | 14 | Set numbersSet = Set(); // Method 2: Using Constructor 15 | numbersSet.add(73); // Insert Operation 16 | numbersSet.add(64); 17 | numbersSet.add(21); 18 | numbersSet.add(12); 19 | 20 | numbersSet.add(73); // Duplicate entries are ignored 21 | numbersSet.add(73); // Ignored 22 | 23 | numbersSet.contains(73); // returns true if the element is found in set 24 | numbersSet.remove(64); // returns true if the element was found and deleted 25 | numbersSet.isEmpty; // returns true if the Set is empty 26 | numbersSet.length; // returns number of elements in Set 27 | // numbersSet.clear(); // Deletes all elements 28 | 29 | print("\n"); 30 | 31 | for (int element in numbersSet) { // Using Individual Element ( Objects ) 32 | print(element); 33 | } 34 | 35 | print("\n"); 36 | 37 | numbersSet.forEach((element) => print(element)); // Using Lambda 38 | } 39 | -------------------------------------------------------------------------------- /35_callable_classes.dart: -------------------------------------------------------------------------------- 1 | 2 | // Objectives 3 | // 1. Callable class 4 | // --> Class treated as Function. 5 | // --> Implement call() method 6 | 7 | void main() { 8 | 9 | var personOne = Person(); 10 | var msg = personOne(25, "Peter"); 11 | print(msg); 12 | } 13 | 14 | class Person { 15 | 16 | String call(int age, String name) { 17 | return "The name of the person is $name and age is $age"; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DartFundamentals.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dart Programming Tutorial for Beginners 2 | Learn Dart Programming, its basics and Fundamentals from scratch. 3 | 4 | ## Topics to be covered 5 | 0. Overview 6 | - Course introduction, prequisites and software required 7 | 1. Installation 8 | - Install required softwares for Windows, MAC and Linux ( Ubuntu ) 9 | 2. Getting Started with Dart Programming 10 | - Run your first app in Dart 11 | - Comments 12 | 3. Exploring Data Types and Variables 13 | - Data Types and Variables 14 | - String, Literals and String Interpolation 15 | - Define constants using "final" and "const" keywords 16 | 4. Control Flow Statements 17 | - IF ELSE 18 | - Conditional Expressions 19 | - Ternary Operator 20 | 5. Loop Control Statements 21 | - What are Iterators? 22 | - FOR Loop and how it works 23 | - WHILE Loop 24 | - DO WHILE Loop 25 | - BREAK statements 26 | - CONTINUE keyword 27 | - Labelled FOR Loop 28 | 6. Exploring Functions or Methods 29 | - Declaring functions 30 | - Function Expressions: Short hand syntax or using FAT ARROR 31 | - Optional Positional Parameters 32 | - Optional Named Parameters 33 | - Optional Default Parameters 34 | 7. Exception Handling 35 | - Demo with example 36 | - Custom Exception Class 37 | 8. Object Oriented Programming: Getting Started 38 | - Defining Class and creating Objects 39 | - Instance and field variables 40 | - Constructors 41 | - Default 42 | - Named 43 | - Parameterized 44 | 9. More on Object Oriented Dart 45 | - Inheritance 46 | - Getter and Setter 47 | - Private Instance Variable 48 | - Polymorphism 49 | - Using constructors in Inheritance 50 | - Static variables and methods 51 | 10. Functional Programming in Dart 52 | - Lambda Expression 53 | - Higher-Order Functions 54 | - Lexical Closures 55 | 11. Dart Collections 56 | - Arrays or List 57 | - Fixed Length List 58 | - Growable List 59 | - Set and HashSet 60 | - Map and HashMap 61 | 12. Callable Classes 62 | 13. Conclusion 63 | 64 | ## Authors 65 | 66 | * **Sriyank Siddhartha** 67 | --------------------------------------------------------------------------------