Problem Statement
What is the purpose of StreamController in Dart?
Explanation
StreamController creates and manages a stream, providing sink for adding data with controller.add(value), adding errors with controller.addError(error), and closing the stream with controller.close(). It's the primary way to create custom streams in Dart when you need to programmatically emit values rather than using built-in stream sources.
Access the stream using controller.stream to listen to it with StreamBuilder or listen() method. StreamController handles subscriber management, buffering events, and proper cleanup. You control when to emit values, making it perfect for implementing custom event buses, wrapping callback-based APIs as streams, or creating reactive data sources.
Always close StreamController when done using it to free resources and prevent memory leaks, typically in dispose() method. Use StreamController.broadcast() for multiple listeners, or regular StreamController() for single listener. StreamController is fundamental for creating custom reactive patterns in Flutter, bridging imperative and reactive programming models.
