Skip to content

Instantly share code, notes, and snippets.

View bboltn's full-sized avatar
👋

Brian Bolton bboltn

👋
View GitHub Profile
@bboltn
bboltn / main.dart
Created April 28, 2019 23:59
sync generators
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;
@bboltn
bboltn / main.dart
Created April 28, 2019 23:48
typedef
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
@bboltn
bboltn / main.dart
Last active April 29, 2019 00:00
streams
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;
@bboltn
bboltn / main.dart
Last active May 5, 2019 11:14
async await
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), () {
@bboltn
bboltn / main.dart
Created April 28, 2019 21:54
mixins
// 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');
}
@bboltn
bboltn / main.dart
Last active April 29, 2019 00:28
functions
// 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());
@bboltn
bboltn / main.dart
Last active April 29, 2019 00:14
built in types
void main () {
//numbers
int warpFactor = 2;
print('Warp: $warpFactor');
// strings
String name = 'Enterprise';
print('StarShip: $name');
@bboltn
bboltn / main.dart
Created April 28, 2019 19:00
cascade notation
class TvShow {
String title;
String producer;
String director;
Set<String> actors;
int length;
@override
String toString() => 'TvShow: title: $title';
}
@bboltn
bboltn / main.dart
Last active April 29, 2019 01:02
generics
// Mission...
// T is the value type
class Mission<T> {
T missionType;
String start;
String destination;
// generic method
T getMission() => missionType;
@bboltn
bboltn / main.dart
Last active May 2, 2019 11:08
exceptions
class RedAlertException implements Exception {
String cause;
RedAlertException(this.cause);
}
class YellowAlertException implements Exception {
String cause;
YellowAlertException(this.cause);
}