Closed as not planned
Description
Repro:
import 'dart:async';
void main() {
var controller = StreamController<int>();
var controller2 = StreamController<int>();
controller2.stream.listen((v) => controller.add(v));
controller2.add(42);
controller.close();
controller2.add(42);
}
This code throws an error:
Connecting to VM Service at ws://127.0.0.1:34837/-D1zY7tPPbM=/ws
Connected to the VM Service.
Unhandled exception:
Bad state: Cannot add event after closing
#0 _StreamController.add (dart:async/stream_controller.dart:605:24)
#1 main.<anonymous closure> (package:bug/bug.dart:6:47)
#2 _RootZone.runUnaryGuarded (dart:async/zone.dart:1609:10)
#3 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:366:11)
#4 _DelayedData.perform (dart:async/stream_impl.dart:542:14)
#5 _PendingEvents.handleNext (dart:async/stream_impl.dart:647:11)
#6 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:618:7)
#7 _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#8 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
#9 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:118:13)
#10 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:185:5)
Exited (255).
Focus on line 1 (not 0) where it tells me the file, function, line and offset of where the error was thrown.
If you use a tear off:
import 'dart:async';
void main() {
var controller = StreamController<int>();
var controller2 = StreamController<int>();
controller2.stream.listen(controller.add);
controller2.add(42);
controller.close();
controller2.add(42);
}
This is the output:
Connecting to VM Service at ws://127.0.0.1:35306/udzDEf1Qq0U=/ws
Connected to the VM Service.
Unhandled exception:
Bad state: Cannot add event after closing
#0 _StreamController.add (dart:async/stream_controller.dart:605:24)
#1 _RootZone.runUnaryGuarded (dart:async/zone.dart:1609:10)
#2 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:366:11)
#3 _DelayedData.perform (dart:async/stream_impl.dart:542:14)
#4 _PendingEvents.handleNext (dart:async/stream_impl.dart:647:11)
#5 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:618:7)
#6 _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#7 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
#8 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:118:13)
#9 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:185:5)
Exited (255).
I have no more reference as to where this happened on my code. I think this should be better handled for better DX. I was struggling for more than half an hour on my Flutter project with many different files and pages when I realized where this could be happening. The uncaught exceptions
also triggers inside the framework code and there is no way for you to go back to your own code.