Created
September 24, 2019 18:07
-
-
Save mileskrell/df90d8f314d7e6c5537e0193d133e571 to your computer and use it in GitHub Desktop.
Small file demonstrating basic Dart language features. Made for a workshop I did about Flutter.
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 Person { | |
String firstName; | |
String lastName; | |
String _favoriteColor; | |
String hobby; | |
set favoriteColor(String newFavorite) { | |
print("My favorite color has changed from $_favoriteColor to $newFavorite"); | |
_favoriteColor = newFavorite; | |
} | |
Person(this.firstName, this.lastName, this._favoriteColor, {this.hobby}); | |
void sayHello() { | |
print("Hi, I'm $firstName" + (lastName != null ? " $lastName" : "")); | |
print("My favorite color is $_favoriteColor, and I like $hobby"); | |
} | |
static void sayGoodbyeTo(String otherPerson) => print("Bye, $otherPerson!"); | |
void countToFive() async { | |
for (int i = 1; i < 6; i++) { | |
print(i); | |
await Future.delayed(Duration(seconds: 1)); | |
} | |
} | |
void sayLetters() { | |
Future.forEach([' a', ' b', ' c', ' d', ' e'], (letter) async { | |
print(letter); | |
await Future.delayed(Duration(seconds: 1)); | |
}); | |
} | |
} | |
void main() { | |
final john = Person("John", "Geller", "blue", hobby: "reading"); | |
john.sayHello(); | |
// john.favoriteColor = "red"; | |
// john.countToFive(); | |
// john.sayLetters(); | |
////////////////////////////////////////////////////////////////////////////// | |
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
// .map((num) => num * 7) | |
// .where((num) => num % 5 == 0) | |
// .forEach((num) => print(num)); | |
////////////////////////////////////////////////////////////////////////////// | |
// final sam = Random().nextBool() ? Person("sam", "johnson", "green") : null; | |
// | |
// print("Sam's favorite hobby is ${sam?.hobby ?? "unknown"}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment