This file contains hidden or 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 C { | |
int? i; // (1) | |
void f() { | |
if (i == null) return; | |
print(i.isEven); // (2) ERROR | |
} | |
} |
This file contains hidden or 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 hidden or 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 hidden or 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
main() { | |
int anInt; // Declared but not initialized... yet. | |
anInt = 1; // Try commenting out this line. | |
print('The value of anInt is $anInt.'); | |
} |
This file contains hidden or 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
// Copyright 2019 the Dart project authors. All rights reserved. | |
// Use of this source code is governed by a BSD-style license | |
// that can be found in the LICENSE file. | |
// Try using `?`, `!`, and `late` to clean up the analysis | |
// errors in this code! | |
class MyClass { | |
late int val; | |
// Sometimes I get an error: Non-nullable instance field 'val' |
This file contains hidden or 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 hidden or 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
final subscription = myStream.listen( | |
(data) { | |
print('Data: $data'); | |
}, | |
onError: (err) { | |
print('Error!'); | |
}, | |
cancelOnError: false, | |
onDone: () { | |
print('Done!'); |
This file contains hidden or 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
final subscription = myStream.listen(...); | |
subscription.pause(); | |
subscription.resume(); | |
subscription.cancel(); |
This file contains hidden or 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
NumberCreator().stream | |
.where((i) => i % 2 == 0) | |
.map((i) => 'String $i') | |
.listen(print); | |
/* | |
OUTPUT: | |
String 2 | |
String 4 | |
String 6 | |
String 8 |
This file contains hidden or 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
NumberCreator().stream | |
.map((i) => 'String $i') | |
.listen(print); | |
/* | |
OUTPUT: | |
String 1 | |
String 2 | |
String 3 | |
String 4 | |
*/ |
NewerOlder