[Flutter]-Single stream vs broadcast stream

Single stream and a broadcast stream may seem very similar, as both examples use a StreamController to emit values and a StreamBuilder widget to listen to the stream and update the UI.

The main difference between a single stream and a broadcast stream is not in the way we create or use the StreamController, but in how the stream is listened to by listeners. In the case of a single stream, only one listener can be actively subscribed to the stream at any given time, while in the case of a broadcast stream, multiple listeners can be subscribed to the stream at the same time.

Here's an example that demonstrates the difference more clearly:

Let's say we want to create an app that displays a live feed of a chat room. Using a single stream, we can have only one user listening to the chat room feed at a time, which means that only one user will be able to see the messages that are sent. On the other hand, if we use a broadcast stream, we can have multiple users listening to the chat room feed at the same time, which means that all users will be able to see the messages that are sent.

In the single stream example, only the user who is currently listening to the chat room feed will receive the messages. If another user wants to see the messages, they will have to wait for the current listener to finish before they can start listening:

// Single stream example StreamController<String> _chatRoomStreamController = StreamController<String>(); // User 1 listens to chat room StreamSubscription<String> _chatRoomSubscription = _chatRoomStreamController.stream.listen((message) { print('User 1 received message: $message'); }); // User 2 tries to listen to chat room but has to wait for User 1 to finish Future.delayed(Duration(seconds: 5), () { StreamSubscription<String> _chatRoomSubscription = _chatRoomStreamController.stream.listen((message) { print('User 2 received message: $message'); }); }); // Send messages to chat room _chatRoomStreamController.add('Hello'); _chatRoomStreamController.add('How are you?'); _chatRoomStreamController.add('I am fine, thank you.');

In the broadcast stream example, multiple users can listen to the chat room feed at the same time, and all users will receive the messages that are sent:

// Broadcast stream example StreamController<String> _chatRoomStreamController = StreamController<String>.broadcast(); // User 1 listens to chat room StreamSubscription<String> _chatRoomSubscription1 = _chatRoomStreamController.stream.listen((message) { print('User 1 received message: $message'); }); // User 2 listens to chat room at the same time as User 1 StreamSubscription<String> _chatRoomSubscription2 = _chatRoomStreamController.stream.listen((message) { print('User 2 received message: $message'); }); // Send messages to chat room _chatRoomStreamController.add('Hello'); _chatRoomStreamController.add('How are you?'); _chatRoomStreamController.add('I am fine, thank you.');

I hope this clears up the difference between a single stream and a broadcast stream.

Nhận xét

Bài đăng phổ biến từ blog này

Flutter 4.5 - Custom theme, font, image

Flutter 1.2 - Flutter Architecture

Flutter 2.5 - First App | Building a Widget Tree