Skip to content

Instantly share code, notes, and snippets.

View kwalrath's full-sized avatar

Kathy Walrath kwalrath

View GitHub Profile
@kwalrath
kwalrath / main.dart
Created June 15, 2021 18:48
URL bug
class C {
int? i; // (1)
void f() {
if (i == null) return;
print(i.isEven); // (2) ERROR
}
}
@kwalrath
kwalrath / main.dart
Created March 11, 2021 18:37 — forked from miquelbeltran/main.dart
Java-to-Dart codelab: CircleMock example
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;
}
@kwalrath
kwalrath / montecarlo.dart
Last active March 23, 2022 01:10 — forked from timsneath/montecarlo.dart
DartPad for dart.dev/overview
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 π.
main() {
int anInt; // Declared but not initialized... yet.
anInt = 1; // Try commenting out this line.
print('The value of anInt is $anInt.');
}
@kwalrath
kwalrath / main.dart
Last active February 21, 2020 22:20
nullsafety1-solution
// 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'
@kwalrath
kwalrath / callable_function.dart
Last active April 8, 2020 23:50 — forked from Sfshaza/callable_function.dart
Callable object
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);
final subscription = myStream.listen(
(data) {
print('Data: $data');
},
onError: (err) {
print('Error!');
},
cancelOnError: false,
onDone: () {
print('Done!');
final subscription = myStream.listen(...);
subscription.pause();
subscription.resume();
subscription.cancel();
NumberCreator().stream
.where((i) => i % 2 == 0)
.map((i) => 'String $i')
.listen(print);
/*
OUTPUT:
String 2
String 4
String 6
String 8
NumberCreator().stream
.map((i) => 'String $i')
.listen(print);
/*
OUTPUT:
String 1
String 2
String 3
String 4
*/