Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for nullable types #20

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.vscode
14 changes: 12 additions & 2 deletions 02_built_in_data_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ void main(List<String> 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.
*/

}
2 changes: 1 addition & 1 deletion 06_conditional_expressions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion 15_optional_positional_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
10 changes: 7 additions & 3 deletions 16_named_parameters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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!}");
}
2 changes: 1 addition & 1 deletion 17_default_parameters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions 18_exception_handling.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -52,7 +52,7 @@ void main() {
try {
depositMoney(-200);
} catch (e) {
print(e.errorMessage());
print(e);
} finally {
// Code
}
Expand Down
2 changes: 1 addition & 1 deletion 19_class_and_objects.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion 20_constructors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions 21_getters_setters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions 22_inheritance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void main() {

class Animal {

String color;
String? color;

void eat() {
print("Eat !");
Expand All @@ -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 !");
Expand All @@ -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 !");
Expand Down
2 changes: 1 addition & 1 deletion 23_method_overriding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Animal {

class Dog extends Animal {

String breed;
String? breed;

String color = "Black"; // Property Overriding

Expand Down
4 changes: 2 additions & 2 deletions 24_inheritance_with_constructors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void main() {

class Animal {

String color;
String? color;

Animal(String color) {
this.color = color;
Expand All @@ -32,7 +32,7 @@ class Animal {

class Dog extends Animal {

String breed;
String? breed;

Dog(String breed, String color) : super(color) {
this.breed = breed;
Expand Down
4 changes: 2 additions & 2 deletions 25_abstract_class_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion 27_static_method_variable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down