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
class StarShip { | |
int warpNacelles; | |
String name; | |
StarShip(this.name, this.warpNacelles); | |
// example of a synchronus generator | |
// We'll show an async generator later. | |
Iterable<int> engage(int warpFactor) sync* { | |
int currentWarp = 1; |
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
class StarShip { | |
double deckCount; // number of decks on the ship | |
int phaserCount; | |
double warpNacelles; | |
String name; | |
@override | |
String toString() => 'Ship: Name: $name, Deck: $deckCount, Phaser: $phaserCount, Nacelles: $warpNacelles'; | |
} | |
// any function that wants to build star ships, must conform to this signature |
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:async'; | |
Stream<String> timeTravelers() async* { | |
var travelers = ['Michael Burhnam', 'Sylvia Tilly', 'Saru', 'Gabrielle Burnham']; | |
for(var traveler in travelers) { | |
await new Future.delayed(Duration(seconds: 2)); | |
if (traveler == 'Gabrielle Burnham') throw 'Time crystal broke! $traveler sent to wrong year!'; | |
// async generator | |
yield traveler; |
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:async'; | |
Future sendToFuture(int year, String person) async { | |
return new Future.delayed(Duration(milliseconds: year), () { | |
print('>> ' + person + ' has arrived in the year $year'); | |
}); | |
} | |
Future<int> whatYearIsIt() async { | |
return new Future.delayed(Duration(milliseconds: 1500), () { |
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
// give behavior to classes with mixins | |
mixin Phasers { | |
void power() => print('Phaser set to stun'); | |
void blast() => print('phaser blast'); | |
} | |
mixin PhotonTorpedo { | |
void launch() => print('photon torpedo launch'); | |
} |
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
// optional named parameter | |
String formatter1(String val, {bool html = false}) => html ? '<span>$val</span>' : val; | |
// optional positional parameter | |
String formatter2(String val, [bool html = false]) => html ? '<span>$val</span>' : val; | |
main() { | |
// inline functions | |
galacticCoord() => 'milky way'; | |
print(galacticCoord()); |
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
void main () { | |
//numbers | |
int warpFactor = 2; | |
print('Warp: $warpFactor'); | |
// strings | |
String name = 'Enterprise'; | |
print('StarShip: $name'); | |
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
class TvShow { | |
String title; | |
String producer; | |
String director; | |
Set<String> actors; | |
int length; | |
@override | |
String toString() => 'TvShow: title: $title'; | |
} |
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
// Mission... | |
// T is the value type | |
class Mission<T> { | |
T missionType; | |
String start; | |
String destination; | |
// generic method | |
T getMission() => missionType; | |
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
class RedAlertException implements Exception { | |
String cause; | |
RedAlertException(this.cause); | |
} | |
class YellowAlertException implements Exception { | |
String cause; | |
YellowAlertException(this.cause); | |
} |
NewerOlder