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'; | |
abstract class Shape { | |
factory Shape(String type) { | |
if (type == 'circle') return Circle(2); | |
if (type == 'square') return Square(2); | |
throw 'Can\'t create $type.'; | |
} | |
num get area; | |
} |
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' show Random; | |
main() async { | |
print('Compute π using the Monte Carlo method.'); | |
await for (final estimate in computePi().take(100)) { | |
print('π ≅ $estimate'); | |
} | |
} | |
/// Generates a stream of increasingly accurate estimates of π. |
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 WannabeFunction { | |
String call(String a, String b, String c) => '$a $b $c!'; | |
} | |
var wf = WannabeFunction(); | |
var out = wf('Hi', 'there,', 'gang'); | |
main() => print(out); |
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
// Add a contravariant variance modifier | |
class Writer<in T> { | |
void write(T x) => print(x); | |
} | |
main() { | |
// Error! Cannot assign Writer<int> to Writer<Object> because the type | |
// parameter is contravariantly declared. | |
Writer<Object> objectWriter = new Writer<int>(); | |
} |
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() { | |
var iterable = ['Salad', 'Popcorn', 'Toast']; | |
for (var element in iterable) { | |
print(element); | |
} | |
} |
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() { | |
Iterable iterable = ['Salad', 'Popcorn', 'Toast']; | |
print('The first element is ${iterable.first}'); | |
print('The last element is ${iterable.last}'); | |
} |
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
bool predicate(String element) { | |
return element.length > 5; | |
} | |
main() { | |
var items = ['Salad', 'Popcorn', 'Toast', 'Lasagne']; | |
// You can find with a simple expression: | |
var element1 = items.firstWhere((element) => element.length > 5); | |
print(element1); |
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
Use the methods `contains` and `startWith` from the `String` class. |
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() { | |
var items = ['Salad', 'Popcorn', 'Toast']; | |
if (items.any((element) => element.contains('a'))) { | |
print('At least one element contains "a"'); | |
} | |
if (items.every((element) => element.length >= 5)) { | |
print('All elements have length >= 5'); | |
} |
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
Use the methods `any` and `every` to compare the user age. |
NewerOlder