Last active
January 29, 2020 01:35
-
-
Save IhwanID/99ae872af49be6cba017a8b33d54c347 to your computer and use it in GitHub Desktop.
Introduction to dart
This file contains hidden or 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'; | |
| void main() { | |
| final square = Square(side: 2.0); | |
| print(square.area); | |
| final circle = Circle(radius:14.0); | |
| printArea(circle); | |
| } | |
| void printArea(Shape shape) => print(shape.area); | |
| abstract class Shape{ | |
| double get area; | |
| } | |
| class Square implements Shape{ | |
| Square({this.side}); | |
| final double side; | |
| double get area => side * side; | |
| } | |
| class Circle implements Shape{ | |
| Circle({this.radius}); | |
| final double radius; | |
| double get area => 2 * pi * radius; | |
| } |
This file contains hidden or 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
| void main() { | |
| final person = Person(name: "Ihwan", height: 1.7); | |
| print(person.sayHello()); | |
| person.intro(); | |
| } | |
| class Person{ | |
| Person({this.name, this.age = 17, this.height}); | |
| final String name; | |
| final int age; | |
| final double height; | |
| String sayHello() => "Hello $name"; | |
| void intro() => print("hello, i'm $name, $age y.o & $height meter tall"); | |
| } |
This file contains hidden or 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
| void main() { | |
| //variable & declaration | |
| String name = 'Ihwan'; | |
| int age = 22; | |
| double height = 1.7; | |
| var friend = 'Andi'; | |
| final phi = 3.14; | |
| dynamic salary = '1000000'; | |
| sayName("udin"); | |
| describe(name, age); | |
| final String andi = describeFriend(friend: friend); | |
| print(andi); | |
| } | |
| //use Optional parameters, nullability and default values | |
| void describe(String name, int age, [double height = 1.5]){ | |
| //String interpolation | |
| print("Hello, I'm $name"); | |
| print ('my name has ${name.length} letters'); | |
| print('my age $age y.o & my height $height meter tall'); | |
| } | |
| //named parameter | |
| String describeFriend({String friend, dynamic salary}){ | |
| salary = 'nothing'; | |
| return "my friend name is $friend \n our salary is $salary"; | |
| } | |
| //arrow operator | |
| void sayName(String name) => print("hello $name"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment