diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..706fd07 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +.vscode diff --git a/02_built_in_data_types.dart b/02_built_in_data_types.dart index 5f1e485..5b76da2 100644 --- a/02_built_in_data_types.dart +++ b/02_built_in_data_types.dart @@ -19,9 +19,19 @@ void main(List arguments) { bool isValid = true; var isAlive = false; + // Nullable types: dataType? varName = null; + int? number = null; + print(score); print(exponents); - // NOTE: All data types in Dart are Objects. - // Therefore, their initial value is by default 'null' + /* + NOTE: by default, the initial value of a data type + cannot be null. unless explicitly specified with the ? symbol. + example: + int number = null; // Throws the error bellow + // Error: The value 'null' can't be assigned to a variable of type + 'int' because 'int' is not nullable. + */ + } diff --git a/06_conditional_expressions.dart b/06_conditional_expressions.dart index c0f3751..8c9a885 100644 --- a/06_conditional_expressions.dart +++ b/06_conditional_expressions.dart @@ -19,7 +19,7 @@ void main() { // If expr1 is non-null, returns its value; otherwise, evaluates and // returns the value of expr2. - String name = null; + String? name = null; String nameToPrint = name ?? "Guest User"; print(nameToPrint); diff --git a/15_optional_positional_params.dart b/15_optional_positional_params.dart index 74d81b8..7c320f3 100644 --- a/15_optional_positional_params.dart +++ b/15_optional_positional_params.dart @@ -20,7 +20,7 @@ void printCities(String name1, String name2, String name3) { } // Optional Positional Parameters -void printCountries(String name1, [String name2, String name3]) { +void printCountries(String name1, [String? name2, String? name3]) { print("Name 1 is $name1"); print("Name 2 is $name2"); diff --git a/16_named_parameters.dart b/16_named_parameters.dart index 24dd43e..f97681e 100644 --- a/16_named_parameters.dart +++ b/16_named_parameters.dart @@ -6,14 +6,18 @@ void main() { print(""); findVolume(10, height: 20, breadth: 5); // Sequence doesn't matter in Named Parameter -} + print(""); + + findVolume(10); +} -int findVolume(int length, {int breadth, int height}) { +void 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}"); + // the ! symbol prevents app crashes when a variable is null + print("Volume is ${length * breadth! * height!}"); } diff --git a/17_default_parameters.dart b/17_default_parameters.dart index 122f30c..cead4fc 100644 --- a/17_default_parameters.dart +++ b/17_default_parameters.dart @@ -13,7 +13,7 @@ void main() { } -int findVolume(int length, {int breadth = 2, int height = 20}) { +void findVolume(int length, {int breadth = 2, int height = 20}) { print("Lenght is $length"); print("Breadth is $breadth"); diff --git a/18_exception_handling.dart b/18_exception_handling.dart index 6f3b486..c6fae2c 100644 --- a/18_exception_handling.dart +++ b/18_exception_handling.dart @@ -13,7 +13,7 @@ void main() { try { int result = 12 ~/ 0; print("The result is $result"); - } on IntegerDivisionByZeroException { + } on UnsupportedError { print("Cannot divide by Zero"); } @@ -52,7 +52,7 @@ void main() { try { depositMoney(-200); } catch (e) { - print(e.errorMessage()); + print(e); } finally { // Code } diff --git a/19_class_and_objects.dart b/19_class_and_objects.dart index 086ccab..f4ab833 100644 --- a/19_class_and_objects.dart +++ b/19_class_and_objects.dart @@ -19,7 +19,7 @@ void main() { // 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 + String ?name; // Instance or Field Variable, default value is null void study() { print("${this.name} is now studying"); diff --git a/20_constructors.dart b/20_constructors.dart index d39ac4a..69a14d6 100644 --- a/20_constructors.dart +++ b/20_constructors.dart @@ -33,7 +33,7 @@ void main() { // Define states (properties) and behavior of a Student class Student { int id = -1; - String name; + String? name; Student(this.id, this.name); // Parameterised Constructor diff --git a/21_getters_setters.dart b/21_getters_setters.dart index 1d28de5..4e3f7f0 100644 --- a/21_getters_setters.dart +++ b/21_getters_setters.dart @@ -16,9 +16,9 @@ void main() { 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 + double _percent = 0; // Private Instance Variable for its own library // Instance variable with Custom Setter void set percentage(double marksSecured) => _percent = (marksSecured / 500) * 100; diff --git a/22_inheritance.dart b/22_inheritance.dart index d1ed8f0..3ad2891 100644 --- a/22_inheritance.dart +++ b/22_inheritance.dart @@ -23,7 +23,7 @@ void main() { class Animal { - String color; + String? color; void eat() { print("Eat !"); @@ -32,7 +32,7 @@ class Animal { 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 !"); @@ -41,7 +41,7 @@ class Dog extends Animal { // Dog is Child class or sub class, Animal is su class Cat extends Animal { // Cat is Child class or sub class, Animal is super or parent class - int age; + int? age; void meow() { print("Meow !"); diff --git a/23_method_overriding.dart b/23_method_overriding.dart index 99011d6..b335793 100644 --- a/23_method_overriding.dart +++ b/23_method_overriding.dart @@ -21,7 +21,7 @@ class Animal { class Dog extends Animal { - String breed; + String? breed; String color = "Black"; // Property Overriding diff --git a/24_inheritance_with_constructors.dart b/24_inheritance_with_constructors.dart index deae98a..543d1ae 100644 --- a/24_inheritance_with_constructors.dart +++ b/24_inheritance_with_constructors.dart @@ -18,7 +18,7 @@ void main() { class Animal { - String color; + String? color; Animal(String color) { this.color = color; @@ -32,7 +32,7 @@ class Animal { class Dog extends Animal { - String breed; + String? breed; Dog(String breed, String color) : super(color) { this.breed = breed; diff --git a/25_abstract_class_method.dart b/25_abstract_class_method.dart index 181a2f3..a335795 100644 --- a/25_abstract_class_method.dart +++ b/25_abstract_class_method.dart @@ -17,8 +17,8 @@ void main() { abstract class Shape { // Define your Instance variable if needed - int x; - int y; + int? x; + int? y; void draw(); // Abstract Method diff --git a/27_static_method_variable.dart b/27_static_method_variable.dart index 0d3f2ce..e52bf46 100644 --- a/27_static_method_variable.dart +++ b/27_static_method_variable.dart @@ -28,7 +28,7 @@ class Circle { 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");