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

committing dart directory #5

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 6 additions & 4 deletions bin/classes.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
void main() {
Basic thing = new Basic(55);
thing.id;
Basic thing = Basic(55);
print(thing.id);
thing.doStuff();

Basic.helper();
Basic
.helper(); // Static function can be called directly from class (instead of using object)
}

// Class name should always start with capital letter
class Basic {
int id;

Expand All @@ -15,5 +17,5 @@ class Basic {
print('Hello my ID is $id');
}

static helper() {}
static helper() {} // Static allows function to be called globally from class. Useful for global functions which does not need internal state of object
}
11 changes: 9 additions & 2 deletions bin/constructors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,35 @@ void main() {

const cir = Circle(radius: 50, name: 'foo');

var p1 = Point.fromMap({'lat': 23, 'lng': 50});
var p2 = Point.fromList([23, 50]);
var p1 = Point.fromMap({'lat': 23.0, 'lng': 50.0});
var p2 = Point.fromList([23.0, 50.0]);
// 2 different ways of constructing the object with the same values
// Frequently used in Dart since multiple ways of creating same object
print(p1);
print(p2);
}

class Rectangle {
final int width;
final int height;
String? name;
late final int area;
// late keyword is used since the value is calculated after the width and height values are assigned later on

// Shape(width, height) {
// this.width = width;
// this.height = height;
// }

Rectangle(this.width, this.height, [this.name]) {
// this.name is an optional parameter, hence it is put in square brackets
area = width * height;
}
}

class Circle {
const Circle({required int radius, String? name});
// Here name is declared as String? since it is an optional character, hence is nullable
}

class Point {
Expand Down
8 changes: 7 additions & 1 deletion bin/extend.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
void main() {}
void main() {
Pug tuffy = Pug();
tuffy.walk();
}

abstract class Dog {
// Declared as 'abstract' since it is used only to provide a template for other classes (in this case 'Pug' class)
void walk() {
print('walking...');
}
Expand All @@ -10,6 +14,8 @@ class Pug extends Dog {
String breed = 'pug';

@override
// @override is Used to mention that method from parent class is being overridden by this function. If it is not used, Dart gives a warning
// Used in Flutter for 'dispose' method - original method is used to clear off an object, but the overridden method can use the original method + do additional disposal functions specific to that class
void walk() {
super.walk();
print('I am tired. Stopping now.');
Expand Down
11 changes: 8 additions & 3 deletions bin/futures.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ import 'dart:async';

void main() {
var delay = Future.delayed(Duration(seconds: 5));
// Runs asynchronously after a delay of 5 seconds for 1 time

delay
.then((value) => print('I have been waiting'))
.catchError((err) => print(err));
.then((value) =>
print('I have been waiting')) // If the event happens successfully
.catchError((err) => print(err)); // If the event is unsuccessful

runInTheFuture();
print(runInTheFuture());
}

Future<String> runInTheFuture() async {
// 'async' is sufficient. No need to declare as a Future, since 'async' automatically tells Dart compiler that a Future is to be returned
var data = await Future.value('world');
// Await pauses execution until the condition is resolved. In this case, no need to use 'then' keyword
// Used extensively in Flutter when you need to pause until an API returns a value (e.g.)

return 'hello $data';
}
2 changes: 1 addition & 1 deletion bin/generics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void main() {
}

class Box<T> {
T value;
T value; // Value is a generic - can be any type of variable - double, list, int, etc.

Box(this.value);

Expand Down
3 changes: 2 additions & 1 deletion bin/interfaces.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ void main() {
e._saySecret();
}

// abstract class Elephant - 'abstract' is used to make a class which cannot be instantiated, but is there for interface
class Elephant {
// Public interface
final String name;
Expand All @@ -21,6 +22,6 @@ class Elephant {
// Public method.
sayHi() => 'My name is $name.';

// Private method.
// Private method. - Start function name with '_' to specify it is private
_saySecret() => 'My ID is $_id.';
}
5 changes: 5 additions & 0 deletions bin/packages.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// Importing 'constructors' class from the same folder used earlier
import 'constructors.dart' as External;
// Importing 'as' will allow us to use our own classes which have the same names as the classes imported from 'constructors'. For example, 'Circle'. Here 'External' is called the namespace
import 'constructors.dart' hide Circle;
// Since we will be defining another 'Circle' class here, we do not want to import 'Circle' from constructors
import 'constructors.dart' show Rectangle;
// 'show' is used to import only that specific class (in this case, 'Rectangle') and no other classes from 'constructors'

class Circle {}

void main() {
Circle();
External.Circle(radius: 10);
// Here we must call 'Circle' class from 'External' namespace, since we have 'hidden' constructor's 'Circle' class

Rectangle(1, 2);
}
9 changes: 8 additions & 1 deletion bin/streams.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ import 'dart:async';

void main() {
var stream = Stream.fromIterable([1, 2, 3]); //.asBroadcastStream();
// Stream is used to handle multiple future events as they unfold over different timelines (e.g., listening to an external database)
// Here stream will listen to a stream of 3 events based on numbers in list
// Stream can be considered a list that unfolds over time
// Here it performs each of the following functions for each element of list individually in sequence(e.g., first for 1st element, then 2nd, etc.)

// Listen to stream and print the event occuring. Stream can only be listened to 1 time. If another listener is present, it will not hear the same event as 1st listener
stream.listen((event) => print(event));

// Multiply each event by 2 & print out.
stream.map((event) => event * 2).listen((event) => print(event));

streamFun();
//streamFun();
// Since streamFun also has another stream, this stream will be combined with execution of streamFun's stream. E.g., First element of stream will go through all the operations until streamFun(), then the first element of streamFun stream will go through the next functions
}

streamFun() async {
Expand Down
3 changes: 2 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ packages:
dependency: "direct dev"
description:
name: lints
url: "https://pub.dartlang.org"
sha256: a2c3d198cb5ea2e179926622d433331d8b58374ab8f29cdda6e863bd62fd369c
url: "https://pub.dev"
source: hosted
version: "1.0.1"
sdks:
Expand Down