Explain StreamController, write a broadcast stream example, and why close it?
This tests Dart stream lifecycle and memory safety. A strong answer uses StreamController.broadcast(), adds data and errors, closes it, and explains unclosed controllers leak memory.
WHAT THIS TESTS: The interviewer wants to see if you understand Dart streams as an async primitive, not just as a consumer API. Specifically they are testing whether you know how to create a stream imperatively, the difference between single-subscription and broadcast streams, how to propagate errors correctly, and why resource cleanup matters in long-running Flutter applications. Senior candidates should demonstrate awareness of memory leaks and isolate lifecycle issues.
A GOOD ANSWER COVERS: Four things in order. First, define StreamController as a programmatic source that lets you push data, error, and done events to listeners. Second, explain that the broadcast constructor is required when multiple listeners must receive the same events, because the default constructor creates a single-subscription stream that throws if listened to twice. Third, describe the event methods: add for data, addError for failures, and close to send a done event and shut down the sink. Fourth, explain that closing is critical because an open controller retains its listeners and any internal timers or resources, which leaks memory and can prevent an isolate from shutting down gracefully.
COMMON WRONG ANSWERS: A major red flag is saying close is optional or only a formality. Another is creating a normal StreamController and trying to listen twice, which causes a StateError at runtime. Candidates sometimes forget that addError accepts an optional StackTrace, or they omit await on close and assume synchronous cleanup. Confusing broadcast with BehaviorSubject or ReplaySubject from RxDart is also a mistake unless the question explicitly asks for those packages.
LIKELY FOLLOW-UPS: The interviewer may ask what happens if you add events after closing, which throws a StateError. They might ask how to pause and resume a stream, which relates to the onPause and onResume callbacks. Another follow-up is how to pipe one stream into a controller, which is done with addStream. They could also ask about sync controllers and when to use them, which is for event-loop sensitive scenarios like DOM events or tests.
ONE CONCRETE EXAMPLE: A solid code sketch looks like this. Declare a controller using the StreamController broadcast factory for integers. Attach two listeners via the stream property listen method, each printing events and errors, so you demonstrate that multiple listeners receive every event. Push integers with the add method. Inject a failure with addError passing an Exception. Finally await the close method and verify isClosed becomes true. In a StatefulWidget you would invoke close inside the dispose method to prevent memory leaks.
Read the original → api.flutter.dev
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.