Last active
October 2, 2019 12:04
-
-
Save bluemyria/0a4effdd4a3982d1a89bf71c7a08d7d1 to your computer and use it in GitHub Desktop.
Dart / Flutter myNotes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
/* | |
abstract class Shape { | |
num get area; | |
} | |
*/ | |
abstract class Shape { | |
factory Shape(String type) { | |
if (type == 'circle') return Circle(2); | |
if (type == 'square') return Square(2); | |
throw 'Can\'t create $type.'; | |
} | |
num get area; | |
} | |
class CircleMock implements Circle { | |
num area; | |
num radius; | |
} | |
class Circle implements Shape { | |
final num radius; | |
Circle(this.radius); | |
num get area => pi * pow(radius, 2); | |
} | |
class Square implements Shape { | |
final num side; | |
Square(this.side); | |
num get area => pow(side, 2); | |
} | |
Shape shapeFactory(String type) { | |
if (type == 'circle') return Circle(2); | |
if (type == 'square') return Square(2); | |
throw 'Can\'t create $type.'; | |
} | |
main() { | |
// final circle = Circle(3.45); | |
// final square = Square(5); | |
/* | |
final circle = shapeFactory('circle'); | |
final square = shapeFactory('square'); | |
*/ | |
final circle = Shape('circle'); | |
final square = Shape('square'); | |
print(circle.area); | |
print(square.area); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
class Rectangle { | |
int width; | |
int height; | |
Point origin; | |
Rectangle({this.origin = const Point(0, 0), this.width = 0, this.height = 0}); | |
@override | |
String toString() => | |
'Origin: (${origin.x}, ${origin.y}), width: $width, height: $height'; | |
} | |
main() { | |
print(Rectangle(origin: const Point(10, 20), width: 100, height: 200)); | |
print(Rectangle(origin: const Point(10, 10))); | |
print(Rectangle(width: 200)); | |
print(Rectangle()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
| |
abstract class Shape { | |
num get area; | |
} | |
| |
class Circle implements Shape { | |
final num radius; | |
Circle(this.radius); | |
num get area => pi * pow(radius, 2); | |
} | |
| |
class Square implements Shape { | |
final num side; | |
Square(this.side); | |
num get area => pow(side, 2); | |
} | |
| |
main() { | |
final circle = Circle(3.45); | |
final square = Square(5); | |
print(circle.area); | |
print(square.area); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String scream(int length) => "A${'a' * length}h!"; | |
main() { | |
final values = [1, 2, 3, 5, 10, 50]; | |
for (var length in values) { | |
print(scream(length)); | |
} | |
// OR | |
values.map(scream).forEach(print); | |
// OR | |
values.skip(1).take(3).map(scream).forEach(print); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://dartpad.dartlang.org/ to test