Skip to content

Instantly share code, notes, and snippets.

@srawlins
Created February 17, 2017 22:59
Show Gist options
  • Save srawlins/1c674f021f3102004565823934153aac to your computer and use it in GitHub Desktop.
Save srawlins/1c674f021f3102004565823934153aac to your computer and use it in GitHub Desktop.
Dart SDK Functions that take closures and don't like null.
import 'dart:async';
import 'dart:collection';
void main() async {
print('Function arguments that cannot be null');
// dart:async
//tryy('runZoned(null)', () => runZoned(null));
//tryy('scheduleMicrotask(null)', () => scheduleMicrotask(null));
await trya('Future.doWhile(null)', () => Future.doWhile(null));
await trya('Future.forEach(_, null)', () => Future.forEach([1], null));
await trya('new Future(null)', () => new Future(null));
await trya('new Future.delayed(_, null)', () => new Future.delayed(new Duration(seconds: 1), null));
await trya('new Future.microtask(null)', () => new Future.microtask(null));
await trya('new Future.sync(null)', () => new Future.sync(null));
// Future.catchError
await trya('Future.then(null)', () => new Future.value(7).then(null));
await trya('Future.whenComplete(null)', () => new Future.value(7).whenComplete(null));
// TODO: many from Stream, Stream Controller, StreamSubscription, StreamTransformer, StreamView, Timer, Zone, ZoneDelegate.
// TODO: dart:collection, dart:convert.
// dart:core
tryy('List.generate(_, null)', () => new List.generate(7, null));
tryy('List.any(null)', () => [1, 2].any(null));
tryy('List.every(null)', () => [1, 2].every(null));
tryy('List.expand(null)', () => [1, 2].expand(null));
tryy('List.firstWhere(null)', () => [1, 2].firstWhere(null));
tryy('List.fold(_, null)', () => [1, 2].fold(5, null));
tryy('List.forEach(null)', () => [1, 2].forEach(null));
tryy('List.lastWhere(null)', () => [1, 2].lastWhere(null));
tryy('List.map(null)', () => [1, 2].map(null));
tryy('List.reduce(null)', () => [1, 2].reduce(null));
tryy('List.singleWhere(null)', () => [1, 2].singleWhere(null));
tryy('List.skipWhile(null)', () => [1, 2].skipWhile(null));
tryy('List.takeWhile(null)', () => [1, 2].takeWhile(null));
tryy('List.where(null)', () => [1, 2].where(null));
tryy('List.removeWhere(null)', () => [1, 2].removeWhere(null));
tryy('List.retainWhere(null)', () => [1, 2].retainWhere(null));
tryy('List.sort(null)', () => [1, 2].sort(null));
tryy('Map.forEach(null)', () => {1: 'A', 2: 'B'}.forEach(null));
tryy('Map.putIfAbsent(_, null)', () => {1: 'A', 2: 'B'}.putIfAbsent(7, null));
tryy('Set.removeWhere(null)', () => new LinkedHashSet.from([1, 2]).removeWhere(null));
tryy('Set.retainWhere(null)', () => new LinkedHashSet.from([1, 2]).retainWhere(null));
tryy('String.replaceAllMapped(_, null)', () => ' x '.replaceAllMapped('x', null));
tryy('String.replaceFirstMapped(_, null)', () => ' x '.replaceFirstMapped('x', null));
// TODO: dart:html, dart:io, dart:isolate,
// TODO: dart:js, dart:js_util, dart:math, dart:mirrors, dart:svg,
// TODO: dart:typed_data, dart:web_audio, dart:web_gl, dart:web_sql
print('');
print('Named function arguments that cannot be null');
// dart:async
await trya('Future.wait(_, cleanUp: null)', () => Future.wait([new Future.value(7), new Future.error(7.0)], cleanUp: null));
await trya('Future.then(_, onError: null)', () => new Future.error(7).then((_) => 6, onError: null));
// TODO: Future.timeout()
// TODO: many from Stream, Stream Controller, StreamSubscription, StreamTransformer, StreamView, Timer, Zone, ZoneDelegate.
// TODO: dart:collection, dart:convert.
// dart:core
tryy('List.firstWhere(orElse: null)', () => [1, 2].firstWhere((e) => e == 7, orElse: null));
tryy('List.lastWhere(orElse: null)', () => [1, 2].lastWhere((e) => e == 7, orElse: null));
tryy('String.splitMapJoin(_, onMatch: null)', () => ' x '.splitMapJoin('x', onMatch: null));
tryy('String.splitMapJoin(_, onNonMatch: null)', () => ' x '.splitMapJoin('x', onNonMatch: null));
// TODO: dart:html, dart:io, dart:isolate,
// TODO: dart:js, dart:js_util, dart:math, dart:mirrors, dart:svg,
// TODO: dart:typed_data, dart:web_audio, dart:web_gl, dart:web_sql
}
void tryy(String thing, fn()) {
try {
var result = fn().toString();
print('${thing.padRight(34)} OK??? (returned $result)');
} catch (e) {
print('${thing.padRight(34)} threw ${e.toString().replaceAll('\n', ' ')}');
}
}
void trya(String thing, fn()) async{
try {
var result = (await fn()).toString();
print('${thing.padRight(34)} OK??? (returned $result)');
} catch (e) {
print('${thing.padRight(34)} threw ${e.toString().replaceAll('\n', ' ')}');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment