sdk
sdk copied to clipboard
Runtime type error in dart pad (compiled by dart2js) but not on VM
The following code runs successfully on VM and Chrome (if compiled with DDC), but fails on dart pad with a runtime error (or in chrome when compiled with dart2js):
// @dart = 2.17
import 'dart:async';
void main() {
final controller = StreamController<Map<String, Object?>>();
controller.stream.listen(print);
addFoo(controller.sink);
print('done');
}
void addFoo(StreamSink<Map<String, Object?>> sink) {
if (sink is StreamSink<Map<String, Object>>) throw 'bad sink'; // Removing this line makes the code work
sink.add({'foo': 'bar'});
}
Expected
_StreamSinkWrapper<Map<String, Object?>>
done
{foo: bar}
Actual
_StreamSinkWrapper<Map<String, Object?>>
Uncaught TypeError: t1.add$1 is not a functionError: TypeError: t1.add$1 is not a function
Reduced example with no import
s:
class Foo<T> {
void blah() => print("Foo");
}
void main() {
final foo = Foo<int?>();
if (foo is Foo<int>) throw 'bad foo';
foo.blah();
}
The static type computation in dart2js doesn't take nullability into account. I'm working on correcting this.