Skip to content

Instantly share code, notes, and snippets.

@mileskrell
Created September 24, 2019 18:07
Show Gist options
  • Save mileskrell/df90d8f314d7e6c5537e0193d133e571 to your computer and use it in GitHub Desktop.
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.
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