Flutter · One codebase, native feel
What is the difference between Future and Stream?
Short answer
A Future completes once with a value or error. A Stream delivers zero or more events over time and can be single-subscription or broadcast.
Futures model one-shot async results such as a network call. Streams model sequences such as WebSocket messages, sensor readings or state changes. A Stream can be consumed with await for, listen or StreamBuilder. The subscription must be cancelled when no longer needed.
class Ticker extends StatefulWidget {
const Ticker({super.key});
@override
State<Ticker> createState() => _TickerState();
}
class _TickerState extends State<Ticker> {
final _controller = StreamController<int>();
StreamSubscription<int>? _subscription;
int _latest = 0;
@override
void initState() {
super.initState();
_subscription = _controller.stream.listen((value) {
setState(() => _latest = value);
});
}
@override
void dispose() {
_subscription?.cancel();
_controller.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Text('Latest: ' + _latest.toString());
}
}- Single-subscription streams allow one listener and buffer events until it arrives
- Broadcast streams allow many listeners but drop events when nobody is listening
- A StreamController must be closed to release resources
- StreamBuilder rebuilds on data, error and done events
- Cancelling a subscription stops delivery but does not necessarily stop the source
Error and completion semantics
A stream can emit an error and continue, or terminate. Future-based APIs complete exactly once. When wrapping a callback API, decide whether errors are terminal. If a stream is broadcast and a widget subscribes late, it will not receive earlier events, so keep the latest state separately when that matters.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1When would you use a broadcast stream?
- 2What happens if you forget to cancel a subscription?
- 3How do StreamBuilder and FutureBuilder differ in rebuild behaviour?
- 4How do you convert a callback-based API into a stream?
Say it in the interview
A Future resolves once, while a Stream emits a sequence of values or errors over time. I use futures for request-response and streams for sockets, sensors and state changes. Streams can be single-subscription or broadcast, and every subscription and controller has a lifecycle I must close. StreamBuilder is convenient, but I keep the controller outside build so it is not recreated.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Flutter mock interview and get scored feedback on depth, clarity and confidence.