Make CastStreamSubscription.onData handle a null callback

Fixes #33166

Change-Id: I52e8bcb6c782c84e8a474b0fc40ada24f31bf602
Reviewed-on: https://dart-review.googlesource.com/55984
Reviewed-by: Nate Bosch <nbosch@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
diff --git a/sdk/lib/internal/async_cast.dart b/sdk/lib/internal/async_cast.dart
index eb59a7d..eee09b9 100644
--- a/sdk/lib/internal/async_cast.dart
+++ b/sdk/lib/internal/async_cast.dart
@@ -32,7 +32,8 @@
   Future cancel() => _source.cancel();
 
   void onData(void handleData(T data)) {
-    _source.onData((S data) => handleData(data as T));
+    _source
+        .onData(handleData == null ? null : (S data) => handleData(data as T));
   }
 
   void onError(Function handleError) {
diff --git a/tests/corelib_2/regress_33166_test.dart b/tests/corelib_2/regress_33166_test.dart
new file mode 100644
index 0000000..03f0faf
--- /dev/null
+++ b/tests/corelib_2/regress_33166_test.dart
@@ -0,0 +1,12 @@
+// Copyright (c) 2018, the Dart project authors.  Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'dart:async';
+import "package:expect/expect.dart";
+
+// Regression test for https://github.com/dart-lang/sdk/issues/33166
+void main() async {
+  var stream = new Stream.fromIterable([1, 2, 3]);
+  Expect.equals(await stream.cast<int>().drain().then((_) => 'Done'), 'Done');
+}